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

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?

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

1 Answer 1


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

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 ...
  • 2
    A .pgpass file is generally preferable. 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
    Oct 16, 2015 at 16:25
  • I would also put the commands into a file and use the -f option to psql.
    – dezso
    Aug 27, 2018 at 15:38

Your Answer

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

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