3
\$\begingroup\$

When my Debian server deploys it can run a shell script so I wanted to make one to install postgreSQL, create a role, create two databases and then import a schema into one.

Can anyone please look at this code and tell me if I have done an ok job?

 # POSTGRES
apt-get install -y postgresql
echo "CREATE ROLE deploy LOGIN ENCRYPTED PASSWORD '$APP_DB_PASS';" | sudo -u postgres psql
su postgres -c "createdb db1 --owner deploy"
su postgres -c "createdb db2 --owner deploy"
service postgresql reload
# IMPORT SQL
psql --username=postgres spider < /etc/schema.sql
rolfl
98.1k17 gold badges219 silver badges419 bronze badges
asked Aug 22, 2013 at 10:15
\$\endgroup\$
1
  • \$\begingroup\$ Personally, I like to use long-options in scripts, even for "obvious" options. \$\endgroup\$ Commented Aug 23, 2013 at 8:17

1 Answer 1

2
\$\begingroup\$

I recommend using psql -c command for the first invocation of psql, or better yet, just use the createuser command.

For the second invocation, you might want psql -f /etc/schema.sql. I would also suggest using the --single-transaction flag, so that in the unlikely event of an error, the failure will be blatantly obvious since the spider database will be empty. (I assume you will also create a database named spider sometime before trying to import data into it.)

answered Aug 23, 2013 at 6:09
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.