3

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?

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

4 Comments

Ok, that works... What is it about the EOF statements that is making it work?
en.wikipedia.org/wiki/End-of-file - a cosy way to specify command I want to run as postgres in multiline
So is the <<EOF bit telling it to just run the next bit of commands until it encounters EOF, at which point the script resumes?
yes, so if you want to run more as postgres, put it between <<EOF and EOF

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.