-1

To save the bash as create_mysql.sh, we can create a database magento and a user magento_user with full privileges by password.

mysqlword="xyzzy"
mysql -u root -p$mysqlword -e"CREATE DATABASE magento;"
mysql -u root -p$mysqlword -e"use magento;"
mysql -u root -p$mysqlword -e"CREATE USER magento_user@localhost IDENTIFIED BY 'password';"
mysql -u root -p$mysqlword -e"GRANT ALL PRIVILEGES ON magento.* TO magento_user@localhost IDENTIFIED BY 'password';"
mysql -u root -p$mysqlword -e"FLUSH PRIVILEGES;"
mysql -u root -p$mysqlword -e"exit"

How to rewrite the bash scripts to do the same task with postgresql, as an automatic script not interactive command in psql?

1
  • What did you try first? Where did you get stuck? Have you looked at any documentation? Commented Oct 16, 2015 at 12:40

1 Answer 1

0

You should use the environment variable PGPASSWORD. In your example that would be

PGPASSWORD="xyzzy"
psql -U root -c 'CREATE DATABASE magento;'

On the command line you can put the environment variable definition before the command:

PGPASSWORD="xyyxxy" psql -U root -c 'CREATE DATABASE magento;'

or export the variable and then use it in subsequent commands

export PGPASSWORD="xxyyzzy"
psql -U root -c 'CREATE DATABASE magento;'
psql ...
3
  • 2
    A .pgpass file is generally preferable. Commented Oct 16, 2015 at 12:39
  • I agree @CraigRinger but I was trying to give an answer which is close in spirit to the MySQL code in the question.
    – jwg
    Commented Oct 16, 2015 at 16:25
  • I would also put the commands into a file and use the -f option to psql. Commented Aug 27, 2018 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.