# Upgrade Postgres to a new major version safely

> A major Postgres upgrade changes the on-disk data format, so you move data with dump and restore or pg_upgrade instead of just installing or starting a newer version.

URL: https://levelrail.com/learn/databases/upgrade-postgres-major-version/
Updated: 2026-11-27

A major Postgres upgrade (for example 16 to 17) cannot be done by installing the new package or changing a Docker tag, because the on-disk data format can change between major versions. You either dump the data and restore it into a new cluster, or run `pg_upgrade` to convert the old cluster in place. In both cases you rehearse on a copy first and keep the old data until the new server has proven itself.

## How it works

Postgres version numbers have a major part and a minor part. Minor releases (16.3, 16.4) contain bug and security fixes, keep the same storage format, and upgrade by installing the new binaries and restarting. A major release can change the internal format, so the new server will not open the old data directory. The server compares a `PG_VERSION` file in the data directory with its own version and exits when they differ.

You have two supported ways to cross a major version:

- **Dump and restore.** [`pg_dump`](https://levelrail.com/learn/glossary/#pg-dump)) and `pg_dumpall` export logical data from the old server, and you load it into a fresh cluster on the new version. It works across any versions, rebuilds every index, and leaves the old cluster intact. Downtime is the dump plus restore time, which grows with database size.
- **pg_upgrade.** It reads the old cluster's catalogs and builds a new cluster, copying data files, or hard-linking them with `--link` for speed. It is much faster for large databases, but with `--link` the old cluster must not be started again after the new one runs.

Plan around your own size and downtime budget, and read the release notes of the target version for incompatible changes and extension requirements. Do not assume how long a version is supported: check the project's versioning policy page.

## Step 1: Prepare and rehearse

1. Take and verify a [backup](https://levelrail.com/learn/glossary/#backup)): `pg_dump -Fc` per database and `pg_dumpall --globals-only` (see [Postgres backup and restore with pg_dump](https://levelrail.com/learn/databases/postgres-backup-and-restore-with-pg-dump/))).
2. List extensions per database with `\dx` in psql. Each one needs a package for the new major version installed.
3. Copy the data to a spare machine or volume, or restore the dump there, and run the whole upgrade on it. Time each step and run your app's tests against the result.
4. Note settings you changed in `postgresql.conf` and `pg_hba.conf`. They are not merged for you.

## Step 2: Upgrade by dump and restore

On a host where both versions are installed (apt keeps versions side by side, each in its own cluster):

```bash
sudo -u postgres pg_dumpall --globals-only > globals.sql
sudo -u postgres pg_dump -Fc shop -f shop.dump
```

Stop the app so no writes arrive, then run the dump. Create the new cluster, load roles, create the database and restore:

```bash
psql -h 127.0.0.1 -p 5433 -U postgres -f globals.sql
createdb -h 127.0.0.1 -p 5433 -U postgres -O app shop
pg_restore -h 127.0.0.1 -p 5433 -U postgres -d shop -j 4 shop.dump
```

Use the new version's `pg_dump` for the dump when you can: it can read older servers, and it is the one the project recommends for upgrades. Point the app at the new port, or swap ports after checks pass. Keep the old cluster stopped but present until you are sure.

## Step 3: Or upgrade with pg_upgrade

On Debian and Ubuntu, the wrapper `pg_upgradecluster` does the sequence for you:

```bash
sudo apt install postgresql-17
pg_lsclusters
sudo pg_upgradecluster 16 main
```

The package name `postgresql-17` stands for your target version. The command creates a new cluster, runs the upgrade, and gives it the port of the old one while moving the old one aside. The old cluster is kept, and you remove it later with `pg_dropcluster 16 main` once you are certain, because that deletes data.

On other systems, call the tool directly with both sets of binaries and data directories, stop both servers, and run `pg_upgrade --check` first. The check mode makes no changes and reports blockers such as missing extensions:

```bash
pg_upgrade --check \
  -b /usr/lib/postgresql/16/bin -B /usr/lib/postgresql/17/bin \
  -d /var/lib/postgresql/16/main -D /var/lib/postgresql/17/main
```

## Step 4: Docker, new image and new volume

Changing `postgres:16` to `postgres:17` on the same volume fails. The container log shows an error that the data directory was initialized by a different Postgres version and is incompatible with the running server, then the container exits or restarts. The data is untouched, so set the old tag back and it starts again. The safe path is dump and restore:

1. Dump from the running old container (`docker exec pg pg_dump -U postgres -Fc shop > shop.dump`).
2. Start a new container from the new tag on a new, empty volume.
3. Restore into it, and switch the app over.
4. Keep the old volume until you are done, then remove it deliberately.

See [update a container to a new image](https://levelrail.com/learn/docker/update-a-container-to-a-new-image/)) for the general flow and [Docker volumes and persistent data](https://levelrail.com/learn/docker/docker-volumes-and-persistent-data/)) for why the volume outlives the container.

## Step 5: Finish up

`pg_upgrade` does not carry over planner statistics, and a fresh restore has none either. Rebuild them:

```bash
vacuumdb --all --analyze-in-stages
```

The `--analyze-in-stages` flag produces rough statistics fast so queries become usable quickly, then refines them. Update extensions with `ALTER EXTENSION name UPDATE;` where needed. Re-check `pg_hba.conf`, `listen_addresses` and your backup job, and confirm `SELECT version();` shows the new major version.

## Check it worked

- `SELECT version();` reports the new major version.
- `pg_lsclusters` shows the new cluster `online` on the expected port.
- Row counts of your main tables match the old server, and the app's health check passes.
- A new backup of the upgraded server restores cleanly.

## Edge cases and gotchas

- **`--link` has no way back.** After the new cluster starts, the old data directory is not safe to use. Take a backup first.
- **Collation changes.** If the operating system's locale library changes, text indexes may need `REINDEX`. Check the release notes when you also change the OS.
- **Extensions.** An extension missing for the new version makes the restore or `pg_upgrade --check` fail. Install packages before you start.
- **Config is not migrated.** Settings in the old `postgresql.conf` do not move. Diff the files and apply your changes by hand.
- **Replicas.** A streaming replica cannot cross a major version. Rebuild it from the new primary.
- **Do not skip the copy test.** Time, disk space and extension problems only show up when you run the real steps.

## Troubleshooting

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| `FATAL:  database files are incompatible with server` | New server on old data directory | Use dump and restore or `pg_upgrade` into a new directory |
| `DETAIL:  The data directory was initialized by PostgreSQL version 16, which is not compatible with this version 17.` | Docker tag changed on an old volume | Revert the tag, then migrate with a dump |
| `Your installation references loadable libraries that are missing from the new installation` | Extension not installed for new version | Install the matching package |
| `pg_upgrade: error: new cluster is not empty` | Target data directory already used | Create a fresh cluster |
| `could not connect to server: No such file or directory` during upgrade | Old server not stopped or sockets mismatch | Stop both, check `-s` socket dir and ports |
| `ERROR:  could not open extension control file` on restore | Extension missing on the target | Install it, then retry |
| Queries slow right after upgrade | No statistics yet | `vacuumdb --all --analyze-in-stages` |
| `pg_upgradecluster` stops because a 17 cluster already exists | The package install created an empty cluster | Check it holds no data with `pg_lsclusters`, remove it with `pg_dropcluster` (permanent), then retry |

## Quick reference

```bash
pg_dumpall --globals-only > globals.sql
pg_dump -Fc shop -f shop.dump
pg_restore -d shop -j 4 shop.dump
pg_lsclusters
sudo pg_upgradecluster 16 main
pg_upgrade --check -b OLDBIN -B NEWBIN -d OLDDATA -D NEWDATA
vacuumdb --all --analyze-in-stages
```

## Related lessons

- [Postgres backup and restore with pg_dump](https://levelrail.com/learn/databases/postgres-backup-and-restore-with-pg-dump/)): the tools this upgrade is built on.
- [Update a container to a new image](https://levelrail.com/learn/docker/update-a-container-to-a-new-image/)): the container side of changing tags.
- [Keep a server updated](https://levelrail.com/learn/server-basics/keep-a-server-updated/)): how minor updates arrive through apt.
- [Rollbacks](https://levelrail.com/learn/deploy-operate/rollbacks/)): plan how to go back if the new version misbehaves.
- [Self-hosted Postgres in Docker, connect your app](https://levelrail.com/blog/self-hosted-postgres-docker-connect-app/)): the Docker setup you may be upgrading.

## Next

Continue with [MySQL and MariaDB basics](https://levelrail.com/learn/databases/mysql-and-mariadb-basics/)) to learn the other common relational server.

## Frequently asked questions

### What is the difference between a minor and a major Postgres upgrade?

A minor release (for example 16.3 to 16.4) keeps the same data format, so you install it and restart. A major release (16 to 17) can change the data directory format and needs dump and restore or pg_upgrade.

### Can I upgrade Postgres by changing the Docker image tag?

Not for a major version. The new container sees a data directory from the old version and refuses to start. Dump the data, start the new image on a fresh [volume](/learn/glossary/#volume), and restore.

### Which is faster, pg_upgrade or dump and restore?

pg_upgrade is usually faster on large databases, especially with --link, because it does not rewrite all data. Dump and restore is simpler and also rebuilds everything cleanly.

### Do I need to run ANALYZE after upgrading Postgres?

Yes. Planner statistics are not carried over by pg_upgrade, so run vacuumdb --all --analyze-in-stages to rebuild them.


## Sources

- [PostgreSQL documentation, upgrading a cluster](https://www.postgresql.org/docs/current/upgrading.html)
- [PostgreSQL documentation, pg_upgrade](https://www.postgresql.org/docs/current/pgupgrade.html)
