In today’s article, we will learn how to migrate the current data directory to a different location in PostgreSQL.
We can follow the steps below.
We learn the current data directory as follows.
1 2 3 4 |
postgres=# show data_directory; data_directory ------------------------------ /var/lib/pgsql/13/data |
The PostgreSQL service is shut down.
1 |
$ sudo systemctl stop postgresql-13 |
We do the migration using rsync.
1 |
$ rsync -av /var/lib/pgsql/13/data /pg_data |
In the postgresql.conf file, data_directory is specified as the newly created directory.
1 2 |
$ vi /var/lib/pgsql/13/data/postgresql.conf data_directory = '/pg_data/data' |
After the processes are completed, we start the service.
1 2 |
$ sudo systemctl start postgresql-13 $ sudo systemctl status postgresql-13 |
Connect to the database and check the data directory.
1 2 3 4 5 |
$ psql postgres=# show data_directory; data_directory ------------------------------ /pg_data/data |