How to change the data directory for PostgreSQL
We use the default data folder when installing PostgreSQL on your server, which is
To change this folder, follow the instructions below.
- Connect to your servers via SSH.
Stop the PostgreSQL service by issuing the following command:
$ (sudo -u postgres pg_ctl stop -D /usr/local/pgsql/data -m i -t 5 || true) && sudo stop postgresql
Make sure that PostgreSQL is no longer running:
$ ps aux | grep pgsql
This command must not return any running PostgreSQL processes.
Make a new directory for your data:
$ mkdir /new/path/folder
Make sure that your new folder is only accessible by the PostgreSQL user:
$ chown postgres /new/path/folder $ chmod 700 /new/path/folder
Move your data from the old folder to new one:
$ mv /usr/local/pgsql/data /new/path/folder
Create a symlink to your new folder from the old one:
$ ln -s /new/path/folder/data /usr/local/pgsql/data
Start the PostgreSQL service again:
$ sudo start postgresql
Your PostgreSQL service should now be working with new data folder.