How to change the data directory for PostgreSQL

We use the default data folder when installing PostgreSQL on your server, which is /usr/local/pgsql/data. To change this folder, follow the instructions below.

  1. Connect to your servers via SSH.
  2. Stop the PostgreSQL service by issuing the following command:
  3. $ (sudo -u postgres pg_ctl stop -D /usr/local/pgsql/data -m i -t 5 || true) && sudo stop postgresql  
  4. Make sure that PostgreSQL is no longer running:
  5. $ ps aux | grep pgsql
    This command must not return any running PostgreSQL processes.

  6. Make a new directory for your data:
  7. $ mkdir /new/path/folder
  8. Make sure that your new folder is only accessible by the PostgreSQL user:
  9. $ chown postgres /new/path/folder
    $ chmod 700 /new/path/folder
  10. Move your data from the old folder to new one:
  11. $ mv /usr/local/pgsql/data /new/path/folder
  12. Create a symlink to your new folder from the old one:
  13. $ ln -s /new/path/folder/data /usr/local/pgsql/data
  14. Start the PostgreSQL service again:
  15. $ sudo start postgresql
    Your PostgreSQL service should now be working with new data folder.

Still need help? Contact Us Contact Us