Moving Postgresql Data Folder

September 10, 2008

We had a old server which ran out space for partition where postgres was running… we had to move the data folder to a bigger partition… and here is how I did it…

Step 0:  Stop the Postgres if running

$service postgresql stop

Step 1 :  Copy  the whole  data/  folder  to  new parition

say  we have  /usr/local/pgsql/data   and wants to move to /home2/ partition

$cp -R /usr/local/pgsql/data  /home2/postgres/data

Step 2 :  Modify the Startup script to point to correct data directory

In  /etc/init.d/postgresql  file,  change the value of  PGDATA  variable to new location  which is /home2/postgres/data

Step 3 : Start the db server

$ service postgresql start

2 Responses to “Moving Postgresql Data Folder”

  1. sean Says:

    this doesn’t work for postgres 8.3 on ubuntu. I don’t know the answer yet, but this comment in that init.d file verifies this:

    # Setting environment variables for the postmaster here does not work; please set them in /etc/postgresql/8.3//environment instead.


  2. A far better solution is to heed how the init script is designed allowing placing overriding config in /etc/sysconfig/pgsql/postgresql

    The directory should have been created by default but the file is not, but it’s just to create it and enter the modified config desired such as:

    PGDATA=/your/desired/path

    Finally, restart PostgreSQL. It worked like a charm for me.


Leave a Reply