3.10. Migrating a PostgreSQL instance from a previous RHEL version to PostgreSQL on RHEL 10
If you already run PostgreSQL on RHEL 9 and want to move the database software to a host that runs RHEL 10, you can migrate the databases.
The following migration methods are available:
- Backup and restore upgrade - This method might require more time but works in most scenarios.
-
Fast upgrade by using the
pg_upgradeutility - This method is faster but works only if you migrate from PostgreSQL 13 to 16 and the hardware architecture stays the same.
Always back up the /var/lib/pgsql/data/ directory on the source host before a PostgreSQL migration.
3.10.1. Migrating to PostgreSQL on RHEL 10 by using the backup and restore method リンクのコピーリンクがクリップボードにコピーされました!
You can use the backup and restore method to migrate data from any RHEL 8 or RHEL 9 version of PostgreSQL to any equal or later version of PostgreSQL on RHEL 10.
Prerequisites
- The existing database server runs on RHEL 8 or RHEL 9 and uses a PostgreSQL version installed from the RHEL repositories.
-
The locale settings on both hosts are the same. To verify this, compare the output of the
echo $LANGcommand on both hosts.
Procedure
On the host with the existing PostgreSQL instance that you want to migrate:
Export all databases to the
/var/lib/pgsql/pgdump_file.sqlfile:# su - postgres -c "pg_dumpall > /var/lib/pgsql/pgdump_file.sql"Check the exported file:
# su - postgres -c 'less "/var/lib/pgsql/pgdump_file.sql"'Copy the database dump that you created in an earlier step and the PostgreSQL configuration files to the RHEL 10 host, for example:
# scp /var/lib/pgsql/pgdump_file.sql \ /var/lib/pgsql/data/pg_hba.conf \ /var/lib/pgsql/data/pg_ident.conf \ /var/lib/pgsql/data/postgresql.conf \ <user>@<rhel_10_host>:/tmp/
On the RHEL 10 host:
Install the
postgresql-serverpackage:# dnf install postgresql-serverInitialize the
/var/lib/pgsql/data/directory:# postgresql-setup --initdbMove the copied configuration files to the
/var/lib/pgsql/data/directory:# mv /tmp/pg_hba.conf \ /tmp/pg_ident.conf \ /tmp/postgresql.conf \ /var/lib/pgsql/data/Ensure a correct ownership of the content in the
/var/lib/pgsql/data/ directory:# chown -R postgres:postgres /var/lib/pgsql/data/Restore the SELinux context on
/var/lib/pgsql/data/:# restorecon -Rv /var/lib/pgsql/data/Enable and start the
postgresqlservice:# systemctl enable --now postgresql.serviceImport the data as the
postgresuser:# su - postgres -c 'psql -f /tmp/pgdump_file.sql postgres'- Verify your databases and ensure that your applications that use the PostgreSQL server work as expected.
3.10.2. Migrating PostgreSQL 13 from a previous RHEL version to PostgreSQL 16 on RHEL 10 by using pg_update リンクのコピーリンクがクリップボードにコピーされました!
If you want to migrate a PostgreSQL 13 instance from a previous RHEL version to PostgreSQL 16 on RHEL 10, you can use the fast upgrade method. With this method, you copy the content of the /var/lib/pgsql/data/ directory to the RHEL 10 host and the pg_update utility converts the databases.
This method works only if your existing PostgreSQL instance is version 13 and the hardware architecture is the same on the source and destination host. In other cases, use the backup and restore method.
Prerequisites
- The existing database server uses PostgreSQL 13.
- The hardware architecture of the current and future server is the same.
The RHEL 10 host has enough free space on the disk that holds the
/var/lib/pgsql/directory.For example, if the size of the directory on the PostgreSQL server want to migrate is 10 GiB, you require at least 20 GiB free disk space on the RHEL 10 host during the migration.
-
The locale settings on both hosts are the same. To verify this, compare the output of the
echo $LANGcommand on both hosts.
Procedure
On the host with the existing PostgreSQL instance that you want to migrate:
Stop the
postgresqlservice:# systemctl stop postgresql.serviceChange into the
/var/lib/pgsql/directory, and back up thedatasubdirectory:# cd /var/lib/pgsql/ # tar -zcf ~/pgdata.bak.tar.gz data/Copy the
~/pgdata.bak.tar.gzarchive to the RHEL 10 host, for example:# scp ~/pgdata.bak.tar.gz <user>@<rhel_10_host>:/tmp/
On the RHEL 10 host:
Install the required packages:
# dnf install postgresql-server postgresql-upgradeThe
postgresql-upgradepackage provides a PostgreSQL 13 server which is required during the migration.-
If you use third party PostgreSQL server modules, build them against both the
postgresql-develandpostgresql-upgrade-develpackages, and install them. Ensure that the
postgresqlservice is stopped:# systemctl stop postgresql.serviceChange into the
/var/lib/pgsql/directory, and extract the backed up data directory from the previous host:# cd /var/lib/pgsql/ # tar -zxf /tmp/pgdata.bak.tar.gzOptional: Remove the
/tmp/pgdata.bak.tar.gzarchive:# rm /tmp/pgdata.bak.tar.gzPerform the upgrade process:
# postgresql-setup --upgradeThe
postgresql-setupshell script renames the/var/lib/pgsql/data/directory to/var/lib/pgsql/data-old/and uses thepg_upgradeutility to migrate the databases to a re-created/var/lib/pgsql/data/directory.重要The
pg_upgradeutility migrates only the databases and not the configuration files. After the migration,/var/lib/pgsql/data/contains only the default.conffiles. If you, previously, had custom configuration files, copy them from the/var/lib/pgsql/data-old/directory and ensure that they are compatible with the new PostgreSQL version.Enable and start the
postgresqlservice:# systemctl enable --now postgresql.serviceClean up and analyze all databases:
# su postgres -c 'vacuumdb --all --analyze-in-stages'- Verify your databases and ensure that your applications that use the PostgreSQL server work as expected.
Optional: Remove the
/var/lib/pgsql/data-old/directory which contains the databases and configuration file from before the migration.# rm -r /var/lib/pgsql/data-old/Optional: Remove the
postgresql-upgradepackage:# dnf remove postgresql-upgrade