Upgrading PostgreSQL Major/Minor Versions
Quick Start
PostgreSQL has two types of version upgrade: minor and major. Their risk and complexity differ substantially.
| Type | Example | Downtime | Data Compatibility | Risk |
|---|---|---|---|---|
| Minor upgrade | 17.2 → 17.3 | Seconds (rolling) | Fully compatible | Low |
| Major upgrade | 17 → 18 | Minutes | Requires data dir upgrade | Medium |
For detailed online migration process, see Online Migration documentation.
| Action | Description | Risk |
|---|---|---|
| Minor Version Upgrade | Update packages, rolling restart | Low |
| Minor Version Downgrade | Rollback to previous minor version | Low |
| Major Version Upgrade | Logical replication or pg_upgrade | Medium |
| Extension Upgrade | Upgrade extension packages and objects | Low |
Minor Version Upgrade
Minor version upgrades, such as 17.2 → 17.3, are the most common upgrade type. They usually contain security patches and bug fixes. The data directory remains fully compatible. Apply the upgrade with a rolling restart.
Strategy: Use a rolling upgrade to minimize service interruption. Upgrade replicas first. Then switch the primary role and upgrade the original primary.
Step 1: Prepare packages
Verify that the local software repository has the latest PostgreSQL packages. Then refresh the node package cache:
Step 2: Upgrade replicas
Upgrade packages on all replicas and verify version:
Restart all replicas to apply new version:
Step 3: Switchover
Execute switchover to transfer primary role to upgraded replica:
Step 4: Upgrade original primary
The original primary is now a replica. Upgrade its packages and restart it:
Step 5: Verify
Confirm all instances have consistent version:
Minor Version Downgrade
In rare cases, you may need to return to the previous PostgreSQL version. For example, the new version may introduce bugs.
Step 1: Get old version packages
Step 2: Execute downgrade
Step 3: Restart cluster
Major Version Upgrade
Major version upgrades, such as 17 → 18, change the data format. They require specialized data migration tools.
| Method | Downtime | Complexity | Use Case |
|---|---|---|---|
| Logical Replication Migration | Seconds (switch) | High | Production, minimal downtime required |
| pg_upgrade In-Place Upgrade | Minutes~Hours | Medium | Test env, smaller data |
For production, use logical replication migration. Create a cluster with the new version and synchronize data through logical replication. Then switch traffic from the old cluster to the new cluster. This gives the shortest downtime and supports rollback. See Online Migration.
Logical Replication Migration
Logical replication is the recommended approach for production major version upgrades. Core steps:
Step 1: Create new version cluster
Step 2: Configure logical replication
Step 3: Wait for sync completion
Step 4: Switch traffic
After you verify that data synchronization is complete, stop application writes to the source. Wait for final synchronization, then move application connections to the new cluster. Drop the subscription and decommission the source:
For detailed migration process, see Online Migration documentation.
pg_upgrade In-Place Upgrade
pg_upgrade is the official PostgreSQL tool for major version upgrades.
Use it for test environments or when longer downtime is acceptable.
An in-place upgrade causes longer downtime and is difficult to roll back. For production, prefer logical replication migration.
Step 1: Install new version packages
Step 2: Stop Patroni
Step 3: Run pg_upgrade
Step 4: Update links and start
Step 5: Post-processing
Extension Upgrade
When you upgrade PostgreSQL, you usually also need to upgrade its extensions.
Upgrade extension packages
Upgrade extension objects
After package upgrade, execute extension upgrade in database:
Before a major version upgrade, verify that all extensions support the target PostgreSQL version. Some extensions may require removal and reinstallation. Check the extension documentation.
Important Notes
- Back up first: Make a complete backup before every upgrade.
- Test first: Verify the upgrade procedure in a test environment.
- Check extensions: Verify that all extensions support the target version.
- Plan rollback: Prepare a rollback plan, especially for major upgrades.
- Monitor closely: Monitor database performance and error logs after the upgrade.
- Keep records: Record all operations and problems during the upgrade.
Related Documentation
- Online Migration: Zero-downtime migration using logical replication
- Patroni Management: Manage cluster with patronictl
- Cluster Management: Cluster creation, scaling, destruction
- Backup Recovery: PostgreSQL backup and recovery
- Extension Management: Extension installation and management
Was this page helpful?
Thanks—your feedback helps us improve this page.
What got in the way? (optional)