3.7. Backing up and restoring PostgreSQL data with physical copies
A physical backup of PostgreSQL data contains files and directories that store the content. This method is typically faster and smaller in size.
3.7.1. Performing a file system backup on a PostgreSQL server リンクのコピーリンクがクリップボードにコピーされました!
A file system-level backup is a fast way to back up a complete PostgreSQL instance. This method requires a shutdown of the postgresql service for data consistency.
A PostgreSQL file system-level backup is specific to an architecture and RHEL major version. You cannot restore data backed up by this method on a different architecture or RHEL major version.
Procedure
Stop the
postgresqlservice:# systemctl stop postgresql.serviceCreate a backup directory, for example:
# mkdir -p /root/postgresql-backup/Back up the
/var/lib/pgsql/directory:# cp -rp /var/lib/pgsql/ /root/postgresql-backup/The
/var/lib/pgsql/contains all essential files of the PostgreSQL database server, including configuration files, data files, and logs.Start the
postgresqlservice:# systemctl start postgresql.service
3.7.2. Restoring a file system backup on a PostgreSQL server リンクのコピーリンクがクリップボードにコピーされました!
If your PostgreSQL instance has been corrupted, and you previously performed a file system backup that includes the data directory, you can restore the instance from this backup.
Prerequisites
- You performed a file system backup on a PostgreSQL server.
The target server must meet the following conditions of the backup source:
- The PostgreSQL version must be identical.
- The system architecture must be identical.
Procedure
Stop the
postgresqlservice:# systemctl stop postgresql.serviceRemove the current
/var/lib/pgsql/directory:# rm -rf /var/lib/pgsql/Restore the data directory from your backup:
# cp -rp /root/postgresql-backup/pgsql/ /var/lib/Ensure the correct ownership of the
/var/lib/pgsql/directory:# chown -R postgres:postgres /var/lib/pgsql/Restore the SELinux context of the
/var/lib/pgsql/directory:# restorecon -Rv /var/lib/pgsql/Start the
postgresqlservice:# systemctl start postgresql.service
Verification
-
Log in as the
postgresuser. Connect to a database:
$ psql <database>Access data in the database:
<database>=# SELECT * FROM <table>;Disconnect from the PostgreSQL service:
<database>=# \q