• Call: +1 (858) 429-9131

Postgresql on EC2

If you are looking forward to migrating your PostgreSQL database to AWS EC2 cloud servers, you can do so by following the simple steps we have posted below. You can make it more reliable and secure from accidental  EC2 instance crashing by allowing the postgresql server to run from an EBS volume. You can also take  a snapshot of the EBS volume which will be stored on AWS storage service s3 which will  make it highly reliable by making it available on multiple AWS availability zones that spans across datacenters.

1 ) Sign up for AWS    EC2 and  S3 services .

2 ) Launch an EC2  instance using AWS  EC2  API tool  or  GUI’s  like  Elastic fox or  AWS   Management Console

3 ) Launch an EBS  and attach it to the instance

4) Check if the attached EBS  is present on the instance

[bash]fdisk -l[/bash]

if /dev/sdh is the name of the EBS volume , it will be present without a valid partition table , so format the  EBS volume  .

[bash]mkfs  -t ext3  /dev/sdh    ( or   mkfs.ext3 /dev/sdh )[/bash]

5 ) Create a directory  /ebs  and mount the EBS volume to   it

[bash]
mkdir  /ebs
mount /dev/sdh /ebs
[/bash]

6 ) Install postgresql server

[bash]yum  install   postgresql-libs   postgresql    postgresql-server[/bash]

7 ) Create  a  new data directory  for postgresql  database  in EBS volume

[bash]mkdir /ebs/pgsql[/bash]

8 ) Stop Postgresql server if it is already running on your instance

[bash]/etc/init.d/postgresql   stop[/bash]

9 ) Move the Postgresql data  directory to the EBS volume

[bash]mv /var/lib/pgsql  /ebs[/bash]

10) Edit the init script  for Postgresql  and give the correct path for  PGDATA and PGLOG

[bash]
vi   /etc/init.d/postgresql
PGDATA=/ebs/pgsql
PGLOG=/ebs/pgsql/pgstartup.log
[/bash]

11 ) Edit  Postgresql  configuration files  to give access to database client , database , & database user .

[bash]
vi   /ebs/pgsql/data/pg_hba.conf
host    all    all    10.250.51.0/32   trust
vi  /ebs/pgsql/data/postgresql.conf
listen_addresses = ‘*’
port = 5432
[/bash]

12) Start the Postgresql server

[bash] /etc/init.d/postgresql start[/bash]


Associated Links

  • Cloud infrastructure
  • Cloud computing
  • Web services
  • Amazon Elastic Compute Cloud
  • EBS
  • Amazon Web Services
  • Eucalyptus

  • 6 Comments

    1. […] The following documentation will give you a clear picture about how to deploy an Apache-Tomcat Load Balanced setup on Amazon EC2 in such a way that all the sensitive datas are stored in their highly reliable storage solutions like EBS and S3 so that you wont lose your data even if your instance crashes. You can see in the below figure that all the application files, static files and images are stored on EBS and are regularly backed up to s3 by taking snapshots. This can be done by creating softlinks for document root of apache and tomcat ( webapps folder ) or you can change the data directory and run it from EBS. To see how to run the database from EBS , Please check our article on how to  run “PostgreSQL _from_EBS” […]

    2. ivan ceras says:

      Hi, im trying to set-up postgresql on amazon e2c but I got lost after step number 4 “Check if the attached EBS is present on the instance”

      Where did you able to launch something like a console or a terminal to issue those unix command?

    3. Shankar S says:

      Hello Ivan ,

      Yes you have to run those commands from the Linux / unix command line . Basically you need to launch a linux instance first and then create an EBS volume and attach it to the Linux instance . Then login to the Linux instance via SSH and then execute those commands .

      Please let us know if you need any furthyer assisatnce .

      regards ,
      Shankar.S

    4. Felipe says:

      Tks a lot for this. Helped me a lot.

    5. Calvin says:

      HI, Thanks for these instructions. I ran into problems in step 9 when trying to move the pgsql data to /ebs and get the following error:

      mv: inter-device move failed: `/var/lib/pgsql’ to `/ebs/pgsql’; unable to remove target: Is a directory

      Undoing step 7 resolved this for me.

    6. Shankar S says:

      Hello Calvin,

      Great that you solved it yourself. You are right there is no need for step 7) which caused the error.

      You could either skip step 7) or you could move only the contents from /var/lib/pgsql/ directory and then change the permission / ownership of the directory /ebs/pgsql/ to the same as that of /var/lib/pgsql/

      mv /var/lib/pgsql/* /ebs/pgsql/
      chown **** /ebs/pgsql/
      chmod **** /ebs/pgsql/

      Thanks for identifying the issue.

      Regards
      Shankar.S

    Leave a Reply