Oliver Burdekin
Oliver Burdekin

Reputation: 1108

How can I use a bash script to run postgres when launching AWS EC2 instance?

I'm testing some set ups for EC2 to run geoserver. When launching the EC2 instance I have a bash script to speed things up. However, when it gets to the point of creating a postgres db it fails. Below is an excerpt of the script and it appears to fail after the second line:

chown postgres /usr/local/pgsql/data
sudo su postgres
initdb -D /usr/local/pgsql/data
postgres -D /usr/local/pgsql/data &
exit

yum install gcc make gcc-c++ libtool libxml2-devel -y
# ..... etc etc

I've SSHed into an instance an run the above code manually, then made an AMI from that instance which works. I'd still like to know how to have a bash script for Amazon linux that can also start postgres.

Upvotes: 1

Views: 594

Answers (1)

Grisha Levit
Grisha Levit

Reputation: 8617

You can't put sudo su postgres in a script to have the subsequent lines be executed by the postgres user. You need to write like:

chown postgres /usr/local/pgsql/data

sudo -u postgres initdb -D /usr/local/pgsql/data
sudo -u postgres -D /usr/local/pgsql/data &

yum install ...

Upvotes: 1

Related Questions