Skip to content

How to move postgresql data directory on Ubuntu Amazon EC2

It is always a good idea to have the data directory on a separate disk. This makes life easier when something goes wrong as you can recover a backup without worrying about data changes which may have happened since the last disk backup.

We have done this for all our servers running on Amazon EC2 for MySQL a while back and today I did the same for postgresql.

Prepare your EBS volume

If you have not done so, create an EBS volume for this purpose using this great guide by Eric Hammond. Come back here once you hit “Configuring MySQL to use the EBS volume” on that page.

Backup your postgresql database

  • Backup your instance using Amazon EC2 console
  • Also backup your postgresql database(s)

    pg_dump dbname>backup.sql

Move postgres data directory to EBS volume

Stop postgres

sudo /etc/init.d/postgres stop

Create a new directory on your EBS volume

sudo mkdir /vol/lib/pg

Move the postgres data folder to the new location

sudo mv /var/lib/postgresql/8.2/main /vol/lib/pg

Create a symbolic link from the existing location

sudo ln –s /vol/lib/pg/main /var/lib/postgresql/8.2/main

Assign postgres user to the symbolic link

sudo chown postgres:postgres –R /var/lib/postgresql/8.2/main

Start postgres

sudo /etc/init.d/postgres start

That’s all. Your Ubuntu postgres server is now running using the data directory on the EBS volume. Note that we did not need to modify the postgresql.conf file since we created a symbolic link from the existing location. Alternatively, you can edit the data_directory entry in postgresql.conf.

Categories: Linux tips.

Comment Feed

2 Responses

  1. Thanks for the nice tutorial. Shouldn’t be
    sudo chown -R postgres:postgres main (placement of -R). A very minor issue, but might thru newbs off.

    GowthamanOctober 1, 2013 @ 9:27 pmReply
    • Thanks for that Gowthaman. In my experience both works but perhaps the order is important on some distros.



Some HTML is OK

or, reply to this post via trackback.