Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Able to create postgres database in command line but not in bash script

I am trying to write a bash script that automates created and filling a database.

I found that I can use these commands to create a database in the command line:

sudo su postgres
psql -c 'CREATE DATABASE routing;'
exit

I see the database afterwards. However when I put these into a shell script and run it (with sudo), the database is not created. Any ideas?

like image 648
wfgeo Avatar asked Jul 26 '26 05:07

wfgeo


1 Answers

after you sudo su you become another user. at this point script will stop executing, until you exit the user, then it will go on executing.

instead if you want to run something as postgres , try smth like this:

sudo su postgres <<EOF
psql -c 'CREATE DATABASE routing;'
EOF
exit
like image 128
Vao Tsun Avatar answered Jul 28 '26 01:07

Vao Tsun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!