Welcome, Guest! Registration


Thursday, 2018-03-22
Main » 2012 » March » 24 » How to install PostgreSQL at Amazon EC2
0:13 AM
How to install PostgreSQL at Amazon EC2

Here is how to make a no frills installation of PostgreSQL to micro image Amazon Elastic Cloud Computing (ec2).

Login to your virtual machine's ssh console.

You need to be root at the first stage of the install:

[root@ip ~]# sudo su -

Then, create dedicated user for postgres (it is highly advised to have one separate user just for the DB, and postgresql won't let you proceed with some things if you are root):

[root@ip ~]# adduser postgres

Then we need to create our "cluster" folder, - place to keep actual data and logs. I prefer /var/pgsql instead of default /var/lib/pgsql

[root@ip ~]# mkdir /var/pgsql

Set postgres ownership to /var/pgsql (added as reminded in comments, thanks folks!):

[root@ip ~]# chown postgres.postgres /var/pgsql

Set permissions:

chmod 750 /var/pgsql

Now, let's proceed to actually installing PostgresSQL (I installed the stock EC2 one):

[root@ip ~]# yum install postgresql-libs postgresql postgresql-server

That's what appeared for the version info:

 Package            Arch    Version           Repository    Size
 postgresql         x86_64  8.4.9-1.13.amzn1  amzn-updates  3.6 M
 postgresql-libs    x86_64  8.4.9-1.13.amzn1  amzn-updates  233 k
 postgresql-server  x86_64  8.4.9-1.13.amzn1  amzn-updates  5.1 M
Transaction Summary

Now it is the time to deal with the cluster paths:

[root@ip ~]# vi /etc/init.d/postgresql

Edit to:


Finally it is the time to create the cluster. You got to be postgres user in order to do that:

[root@ip ~]# su postgres
[postgres@ip root~]# initdb -D /var/pgsql/data
[postgres@ip ~]# exit

Now we are back root, and it is time to actually start the postgresql service:

[root@ip ~]# service postgresql start

Confirm it is actually running:

[root@ip ~]# service postgresql status

If it is Ok, then it is fine to add it to "autostart" on boot:

sudo chkconfig postgresql on

Additional Info

  1. Postgresql on EC2 - http://www.migrate2cloud.com/blog/postgresql-on-ec2
  2. PostgreSQL 8.4 docs - http://www.postgresql.org/docs/8.4/interactive/index.html
Views: 25541 | Added by: loc2logg | Tags: install, amazon ec2, postgresql, Linux | Rating: 4.5/2
Total comments: 7
I had to make a few changes to install postgres

1) permissions
After the mkdir /var/pgsql the directory should be given to the postgres user:

[root@ip ~]# chown postgres /var/pgsql

it should be PGDATA=/var/pgsql/data in stead of PGDATA=/var/pgsql


Why is it highly advised to have one separate user just for the DB

Great tutorial. I have one issue though.. When I run the "service postgresql start " command I get the following error
/var/pgsql is missing. Use "service postgresql initdb" to initialize the cluster first. I have checked and the folder is there. The only thing that I did extra was to chmod on the pgsql folder for write permissions


Hello Natalie,

Unfortunately I have no suitable box to try things right now and, I am not quite sure on which version and platform you are; but I can still think of bunch of things assuming you are on EC or just some sort of Linux (minding you have chmod), and not cygwin:
1. Missed setting environment variables.
Please make sure
are in /etc/init.d/postgresql
2. /var/pgsql/, /var/pgsql/data do not belong to postgres user and group. It is important to execute the initdb under postgres user.
3. When setting directories by yourself, you might have set read permissions, but removed executable flag on them. These directories shall have rwx permissions for postgres user and group.
4. Maybe I was wrong saying initdb -D /var/pgsql/data. Once you have edited the /etc/init.d/postgresql you may try initdb without any paths, or try initdb -D /var/pgsql

I hope this helps, let me know if any of my hints work (or not smile )


Thanks for this tutorial. I have the same message like Natalie. I checked in /etc/init.d/postgresql and all is correct, but when i execute "service postgresql start" i get one message that says "Initializing database: [FAILED]"

Thnaks for your time.

Should also install "postgresql-devel" for installing pg gem

Good point. Thanks!

Only registered users can add comments.
[ Registration | Login ]