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.
- Connect to your servers via SSH.
- Stop the PostgreSQL service by issuing the following command:
- Make sure that PostgreSQL is no longer running:
- Make a new directory for your data:
- Make sure that your new folder is only accessible by the PostgreSQL user:
- Move your data from the old folder to new one:
- Create a symlink to your new folder from the old one:
- Start the PostgreSQL service again:
$ (sudo -u postgres pg_ctl stop -D /usr/local/pgsql/data -m i -t 5 || true) && sudo stop postgresql
$ ps aux | grep pgsqlThis command must not return any running PostgreSQL processes.
$ mkdir /new/path/folder
$ chown postgres /new/path/folder $ chmod 700 /new/path/folder
$ mv /usr/local/pgsql/data /new/path/folder
$ ln -s /new/path/folder/data /usr/local/pgsql/data
$ sudo start postgresqlYour PostgreSQL service should now be working with new data folder.