PostgreSQL pg_upgrade Guide
February 13, 2026
|
PostgreSQL
Upgrade
Migration
Zero-stress pg_upgrade with link mode.
Upgrading PostgreSQL with pg_upgrade
Major version upgrades (e.g., PostgreSQL 15 → 16) require pg_upgrade. Unlike minor updates, major upgrades change the internal data storage format. pg_upgrade handles this conversion efficiently, especially with --link mode that creates hard links instead of copying data files.
Pre-Upgrade Checklist
- Take a full backup (pgBackRest or pg_basebackup)
- Check extension compatibility with the target version
- Review the release notes for breaking changes
- Test the upgrade on a staging environment first
- Plan for downtime (typically 5-30 minutes with link mode)
Link Mode vs Copy Mode
| Mode | Speed | Disk Space | Rollback |
|---|---|---|---|
--link | Seconds (for any size DB) | No extra space needed | Cannot rollback (old cluster is modified) |
--copy (default) | Proportional to data size | 2x disk space required | Old cluster preserved as-is |
Step-by-Step Upgrade
# 1. Install the new PostgreSQL version
sudo apt install postgresql-16
# 2. Stop both clusters
sudo systemctl stop postgresql
# 3. Run pg_upgrade with link mode
sudo -u postgres pg_upgrade \
--old-datadir=/var/lib/postgresql/15/main \
--new-datadir=/var/lib/postgresql/16/main \
--old-bindir=/usr/lib/postgresql/15/bin \
--new-bindir=/usr/lib/postgresql/16/bin \
--link --check # Dry run first
# 4. If check passes, run for real (remove --check)
sudo -u postgres pg_upgrade \
--old-datadir=/var/lib/postgresql/15/main \
--new-datadir=/var/lib/postgresql/16/main \
--old-bindir=/usr/lib/postgresql/15/bin \
--new-bindir=/usr/lib/postgresql/16/bin \
--link
# 5. Start the new version
sudo systemctl start postgresql@16-main
# 6. Run post-upgrade analyze
sudo -u postgres /usr/lib/postgresql/16/bin/vacuumdb --all --analyze-in-stages
Post-Upgrade Tasks
- Run
ANALYZEon all databases to update statistics - Test application queries against the new version
- Remove old cluster files once verified:
./delete_old_cluster.sh - Update connection strings and monitoring to point to the new port/version
Eazy SaaS Tip: Always use
--checkmode first. It validates everything without making changes. For databases over 500 GB, link mode is essential — copy mode would take hours of downtime.