This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this post, I am sharing complete steps for moving main PostgreSQL data directory to any new location in Linux.
When we install PostgreSQL on Linux operating system, by default it stores in “/var/lib/postgresql/[version]/data/”
Always best practice is to store database data on the separate drive instead of the system drives.
Check the current data directory:
1 2 3 4 5 |
postgres=# SHOW data_directory; data_directory ------------------------------ /var/lib/postgresql/9.6/main |
Stop the PostgreSQL Service:
1 |
sudo systemctl stop postgresql |
Create a new directory:
1 |
mkdir postgres_data_dir |
Give directory ownership to your Linux user:
1 |
chown user_name postgres_data_dir |
Move existing PostgreSQL data/floder to the new location:
1 |
sudo rsync -av /var/lib/postgresql /test/postgres_data_dir/ |
Move old folder to the backup folder:
1 |
sudo mv /var/lib/postgresql/9.6/main /tmp/pg_backup |
Change data folder in postgresql.conf file:
1 |
data_directory = '/test/postgres_data_dir/postgresql/9.6/main' |
Start the service of PostgreSQL:
1 |
sudo systemctl start postgresql |
Now check the data directory:
1 2 3 4 5 |
postgres=# SHOW data_directory; data_directory -------------------------------------------- /test/postgres_data_dir/postgresql/9.6/main |
Leave a Reply