
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
- Install PostgreSQL 15: Install the new PostgreSQL version alongside the old one without removing PostgreSQL 11.
- Prepare the Environment:
- Stop the PostgreSQL 11 server.
- Ensure both versions have the same locale and encoding settings.
- 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.
- 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
- Post-Upgrade Steps:
- First, start the PostgreSQL 15 server.
- Then, Run
ANALYZEto update statistics. - Finally, Check for any deprecated features or compatibility issues.
- 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.
Running this in production?
MinervaDB provides PostgreSQL Consulting, PostgreSQL Support and PostgreSQL Remote DBA with 24x7 coverage and a 15-minute S1 response. Talk to an engineer.