Welcome, Guest! Registration

loc2log

Friday, 2024-03-29
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
===================================================================
Installing:
 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:

PGDATA=/var/pgsql
PGLOG=/var/pgsql/pgstartup.log

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: 27774 | Added by: loc2logg | Tags: postgresql, aws, ec2, install, Linux | Rating: 4.5/2
Total comments: 7
7 Rob Gansevles  
0
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

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

Rob

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

3 Natalie  
0
Hello,
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

Thanks
natalie

4 ep  
0
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
PGDATA=/var/pgsql
PGLOG=/var/pgsql/pgstartup.log
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 )

6 gusfhdz83  
0
Hi,

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.

1 Hien Vo  
1
Should also install "postgresql-devel" for installing pg gem

2 ep  
0
Good point. Thanks!

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