You're on PostgreSQL 13 or 14, it's approaching end of life, and the database is 2 TB. pg_dump and restore would take a day of downtime. How do teams actually upgrade to a new major version (for example 14 → 18)?
There are three main methods. Which one fits depends on how much downtime you can accept.
Minor vs. major upgrades
- Minor (17.2 → 17.6): bug and security fixes, same data format. Install the new binaries and restart. It takes seconds. Do these regularly.
- Major (14 → 18): the on-disk format of the system catalogs changes, so you need
pg_upgrade, a dump and restore, or replication into a new cluster.
Option 1: pg_dump / pg_restore
pg_dump -Fd -j 8 -f /backup/app app
pg_restore -d app -j 8 /backup/app
It's simple and works across any versions and platforms, and it also rebuilds indexes and removes bloat. But downtime lasts for the whole dump and restore, which is fine for tens of GB and painful for terabytes.
Option 2: pg_upgrade (minutes of downtime)
pg_upgrade creates a cluster for the new version and reuses your existing data files, converting only the system catalogs.
# New version installed, new cluster initialized (same encoding, locale, checksums setting)
pg_upgrade \
--old-bindir /usr/lib/postgresql/14/bin --new-bindir /usr/lib/postgresql/18/bin \
--old-datadir /var/lib/postgresql/14/main --new-datadir /var/lib/postgresql/18/main \
--link --jobs 8 --check # --check first: a dry run of compatibility checks
Then run it again without --check, with both clusters stopped.
--linkhard-links data files instead of copying them, so even multi-terabyte clusters take minutes. The catch: once the new cluster has started, the old one can't safely be used any more. Take a backup or snapshot first. (--clone, or--copyon filesystems without reflinks, keeps the old cluster intact but takes longer or more space.)
