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:

重要

Always back up the /var/lib/pgsql/data/ directory on the source host before a PostgreSQL migration.

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 $LANG command on both hosts.

Procedure

  1. On the host with the existing PostgreSQL instance that you want to migrate:

    1. Export all databases to the /var/lib/pgsql/pgdump_file.sql file:

      # su - postgres -c "pg_dumpall > /var/lib/pgsql/pgdump_file.sql"
    2. Check the exported file:

      # su - postgres -c 'less "/var/lib/pgsql/pgdump_file.sql"'
    3. 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/
  2. On the RHEL 10 host:

    1. Install the postgresql-server package:

      # dnf install postgresql-server
    2. Initialize the /var/lib/pgsql/data/ directory:

      # postgresql-setup --initdb
    3. Move 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/
    4. Ensure a correct ownership of the content in the /var/lib/pgsql/data/ directory:

      # chown -R postgres:postgres /var/lib/pgsql/data/
    5. Restore the SELinux context on /var/lib/pgsql/data/:

      # restorecon -Rv /var/lib/pgsql/data/
    6. Enable and start the postgresql service:

      # systemctl enable --now postgresql.service
    7. Import the data as the postgres user:

      # su - postgres -c 'psql -f /tmp/pgdump_file.sql postgres'
    8. Verify your databases and ensure that your applications that use the PostgreSQL server work as expected.

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 $LANG command on both hosts.

Procedure

  1. On the host with the existing PostgreSQL instance that you want to migrate:

    1. Stop the postgresql service:

      # systemctl stop postgresql.service
    2. Change into the /var/lib/pgsql/ directory, and back up the data subdirectory:

      # cd /var/lib/pgsql/
      # tar -zcf ~/pgdata.bak.tar.gz data/
    3. Copy the ~/pgdata.bak.tar.gz archive to the RHEL 10 host, for example:

      # scp ~/pgdata.bak.tar.gz <user>@<rhel_10_host>:/tmp/
  2. On the RHEL 10 host:

    1. Install the required packages:

      # dnf install postgresql-server postgresql-upgrade

      The postgresql-upgrade package provides a PostgreSQL 13 server which is required during the migration.

    2. If you use third party PostgreSQL server modules, build them against both the postgresql-devel and postgresql-upgrade-devel packages, and install them.
    3. Ensure that the postgresql service is stopped:

      # systemctl stop postgresql.service
    4. Change 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.gz
    5. Optional: Remove the /tmp/pgdata.bak.tar.gz archive:

      # rm /tmp/pgdata.bak.tar.gz
    6. Perform the upgrade process:

      # postgresql-setup --upgrade

      The postgresql-setup shell script renames the /var/lib/pgsql/data/ directory to /var/lib/pgsql/data-old/ and uses the pg_upgrade utility to migrate the databases to a re-created /var/lib/pgsql/data/ directory.

      重要

      The pg_upgrade utility migrates only the databases and not the configuration files. After the migration, /var/lib/pgsql/data/ contains only the default .conf files. 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.

    7. Enable and start the postgresql service:

      # systemctl enable --now postgresql.service
    8. Clean up and analyze all databases:

      # su postgres -c 'vacuumdb --all --analyze-in-stages'
    9. Verify your databases and ensure that your applications that use the PostgreSQL server work as expected.
    10. 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/
    11. Optional: Remove the postgresql-upgrade package:

      # dnf remove postgresql-upgrade
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2026 Red Hat
返回顶部