Step-by-Step Guide to Migrating from PostgreSQL 11 to 15 Using pg_upgrade

Introduction to pg_upgrade

pg_upgrade is a powerful tool that allows you to upgrade your PostgreSQL database in-place, minimizing downtime and avoiding the need for a full data dump and restore. It efficiently migrates system catalogs and user data, making it an ideal choice for major version upgrades.

To use pg_upgrade for migrating from PostgreSQL 11 to PostgreSQL 15, follow these steps:

Runbook to Migrate from PostgreSQL 11 to 15 using pg_upgrade

  1. Install PostgreSQL 15: Install the new PostgreSQL version alongside the old one without removing PostgreSQL 11.
  2. Prepare the Environment:
    • Stop the PostgreSQL 11 server.
    • Ensure both versions have the same locale and encoding settings.
  3. Run pg_upgrade:
    • Use the pg_upgrade tool provided with PostgreSQL 15.
    • Run it as the same user owning the database processes.
    • Specify the old and new data directories and server binaries.
  4. Example Command:
    pg_upgrade \\\\
      --old-datadir=/var/lib/pgsql/11/data \\\\
      --new-datadir=/var/lib/pgsql/15/data \\\\
      --old-bindir=/usr/lib/postgresql/11/bin \\\\
      --new-bindir=/usr/lib/postgresql/15/bin
    
  5. Post-Upgrade Steps:
    • First, start the PostgreSQL 15 server.
    • Then, Run ANALYZE to update statistics.
    • Finally, Check for any deprecated features or compatibility issues.
  6. Testing:
    • First, Test your applications against the new server.
    • Finally, Monitor for performance and stability.

Conclusion

Make sure to back up your data before starting the pg_upgrade process to prevent any data loss. Additionally, testing the upgrade in a non-production environment first is highly recommended to identify potential issues before deploying to production. After upgrading, carefully review logs, verify database integrity, and monitor performance to ensure a smooth transition. Lastly, stay updated with PostgreSQL release notes to address any new compatibility changes or optimizations that may further enhance your database performance.

About Shiv Iyer 500 Articles
Open Source Database Systems Engineer with a deep understanding of Optimizer Internals, Performance Engineering, Scalability and Data SRE. Shiv currently is the Founder, Investor, Board Member and CEO of multiple Database Systems Infrastructure Operations companies in the Transaction Processing Computing and ColumnStores ecosystem. He is also a frequent speaker in open source software conferences globally.