2

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?

Vao Tsun
  • 47,234
  • 13
  • 100
  • 132
wfgeo
  • 2,716
  • 4
  • 30
  • 51

1 Answers1

3

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
Vao Tsun
  • 47,234
  • 13
  • 100
  • 132