HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Automating the install of PostgreSQL from Shell

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
postgresqltheinstallshellautomatingfrom

Problem

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

Solution

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.)

Context

StackExchange Code Review Q#30072, answer score: 2

Revisions (0)

No revisions yet.