Skip to content
STAGE33R

Guides · Server software · updated 3 Dec 2026

How to upgrade PostgreSQL to a new major version on Debian and Ubuntu (pg_upgradecluster)

PostgreSQL major versions are supported for about five years, then stop getting security fixes. On Debian and Ubuntu the postgresql-common tools let two versions live side by side, so you can upgrade, test and keep the old cluster until you are sure.

  1. Check what is installed and who uses it

    Applications must be stopped or in maintenance during the upgrade.

    pg_lsclusters
    sudo ss -tnp | grep ':5432'
  2. Back up everything

    A logical backup of all databases and roles, kept outside the data directory.

    sudo -u postgres pg_dumpall > ~/pg-all-$(date +%F).sql
    ls -lh ~/pg-all-*.sql
  3. Install the new version

    It creates an empty cluster next to the old one.

    sudo apt-get install postgresql-16
    pg_lsclusters
  4. Upgrade the cluster

    This copies the data to a new cluster of the new version, which takes the old one's port. Use -m link on large databases for a much faster upgrade (the old cluster then cannot be started again).

    sudo pg_dropcluster 16 main --stop   # the empty cluster the install created
    sudo pg_upgradecluster -m upgrade 14 main
    pg_lsclusters
  5. Test the applications

    Start them and exercise the main flows. Run ANALYZE so the planner has fresh statistics.

    sudo -u postgres vacuumdb --all --analyze-in-stages
  6. Remove the old cluster when you are sure

    Keep it for a few days if there is any doubt.

    sudo pg_dropcluster 14 main
    sudo apt-get purge postgresql-14

Common mistakes

  • Extensions (PostGIS, pgvector) must be installed for the new version first.
  • Connection strings with a hard-coded port after the old cluster was moved to another port.
  • Dropping the old cluster before the applications were tested.

How to undo it

Until you drop it, the old cluster still exists with its data: stop the new one and start the old one again (check ports with pg_lsclusters). After dropping it, restore from pg_dumpall.

Sources

Commands are for Debian and Ubuntu unless noted. Test on a copy first and keep a backup: you are responsible for changes on your own systems.