Always ensure the data integrity before proceeding for the migration. Take the data backup and for safer side please ensure to take the snapshot of the vm.
# Backup command
Postgres Default path:
To check the current running directory of Database using the command below
# sudo -u postgres psql -c "SHOW data_directory;"
Example:
# sudo -u postgres psql -c "SHOW data_directory;"
data_directory
------------------------
/var/lib/pgsql/14/data
(1 row)
Migration of Data from one File System to another:
We can able to migrate the data using rsync method,
# rsync -av /var/lib/pgsql/14/data /new_Filesystem_path
Changing Permissions:
Once the Data copied with attributes by rsync, we need to change the owner of new directory
# chown -R postgres:postgres /new_Filesystem_path
Pointing the New filesystem for Postgres:
According with Postgres Version, Path of conf file has been located. For Instance, we are using version 14.
# vi /var/lib/pgsql/14/data/postgresql.conf
Change the New Filesystem path in below line located in FILE LOCATIONS Heading and remove the # to enable that line and save it.
. . .
data_directory = '/new_Filesystem_path'
. . .
After postgresql.conf file has been changed and saved. We need to restart the postgresql service.
# systemctl restart postgresql-14
Verification of Postgres data Status:
After Saved We can check the path by below command.
# sudo -u postgres psql -c "SHOW data_directory;"
For Permanent Change:
The above changes where only temporary If system restarted, the configuration back to default. So To do permanent We need to change the systemd file.
# vi /usr/lib/systemd/system/postgresql-14.service
And change the line to New file system, For your reference find below
. . .
# Location of database directory
Environment=PGDATA=/new_Filesystem_path/data
. . .
Restarting the Serivces:
Then need to reload the daemon for source path change and restart the postgresql service also
# systemctl daemon-reload
# systemctl restart postgresql-14
Once Everything done, if required we can restart the system to check whether postgresql service will run after booting.
Removing the old Data Directory:
Once Everything up and running, we can remove the old data directory if required to release the storage.
# rm -rf var/lib/pgsql/14/data
Comments
0 comments
Please sign in to leave a comment.