Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What are the main ways to upgrade PostgreSQL from version 15 to 17, and why can you not just install new binaries over the old data directory?

A major version changes the on-disk format of the system catalogs, so PostgreSQL 17 binaries cannot open a PostgreSQL 15 data directory, while minor versions keep the same format and only need a restart. The three main ways to upgrade are pg_dump and pg_restore, which is simple but slow for large databases, pg_upgrade, which builds new catalogs and reuses the data files for minutes of downtime, and logical replication to a new cluster followed by a switchover, which gives near-zero downtime with more moving parts. Afterwards we run ANALYZE, because planner statistics are not carried over, except that PostgreSQL 18's pg_upgrade keeps most of them.

Why this question is asked

Every PostgreSQL major version is supported for about five years, so every team running PostgreSQL has to do major upgrades regularly. The method we choose decides how long the application is down and how much can go wrong. Interviewers ask this to see whether we understand why major upgrades need a special process, and which method fits which database. In this answer, we will recap the difference between major and minor versions, then go through the three methods, see what each costs, and finish with the step that people forget after the upgrade.

1Why New Binaries Cannot Use the Old Data Directory

PostgreSQL version numbers have two parts. In 17.2, 17 is the major version and 2 is the minor version. Minor releases contain bug and security fixes only, and they never change the on-disk format, so upgrading from 17.1 to 17.2 means installing new binaries and restarting.

Major versions do change the format. The system catalogs, the tables where PostgreSQL stores its own metadata such as pg_class and pg_attribute, gain and lose columns between majors, and internal structures can change too. Each data directory records the version that created it, and a server of a different major version refuses to start on it with an error that the database files are incompatible with the server. So we cannot just install 17 over 15: something has to move the data into a 17-format cluster.

Shell
$ cat /var/lib/postgresql/15/main/PG_VERSION
15
$ /usr/lib/postgresql/17/bin/pg_ctl -D /var/lib/postgresql/15/main start
# FATAL: database files are incompatible with server
# DETAIL: The data directory was initialized by PostgreSQL version 15, which is not compatible with this version 17.2.

2Method 1: pg_dump and pg_restore

The simplest method exports everything as SQL or an archive with pg_dump, ideally using the pg_dump from the new version, and loads it into a fresh 17 cluster with pg_restore. It works across any versions, and it also cleans up bloat, because the new cluster is written from scratch.

The cost is time. Dumping and restoring means reading every row and rebuilding every index, and writes must stop during the dump to avoid losing changes. With parallel jobs (pg_dump -j with the directory format, pg_restore -j), a 50 GB database might take 30 minutes; a 2 TB database could take most of a day. So this method fits small databases or ones that can be down for a while.

Java
pg_dump -Fd -j 8 -f /backup/shop -d shop # with the version 17 pg_dump
pg_restore -j 8 -d shop /backup/shop # into the new 17 cluster

3Method 2: pg_upgrade

pg_upgrade is the standard tool for large databases. It creates a new, empty 17 cluster, dumps and restores only the schema, which is quick, and then reuses the existing data files instead of rewriting them, because the format of user table and index files usually stays compatible across majors. In its default mode it copies the data files; with --link it creates hard links, and with --clone it uses copy-on-write file cloning where the file system supports it, so even a multi-terabyte database is upgraded in minutes.

We always run it with --check first, which reports problems such as missing extensions in the new installation without changing anything. With --link, the old cluster must not be started again after the new one starts, because both share the same files, so we take a backup or have a standby before the upgrade.

Text
pg_upgrade --check \
-b /usr/lib/postgresql/15/bin -B /usr/lib/postgresql/17/bin \
-d /var/lib/postgresql/15/main -D /var/lib/postgresql/17/main
# *Clusters are compatible*
pg_upgrade --link -b ... -B ... -d ... -D ... # minutes, even for terabytes

4Method 3: Logical Replication and a Switchover

For near-zero downtime, we build a new 17 cluster next to the old one and keep it in sync with logical replication, which sends row changes and works across major versions. Once the new cluster has caught up, we stop writes for a short moment, wait for the last changes to arrive, copy sequence values, and point the applications at the new cluster. The downtime is seconds to a few minutes.

The price is more moving parts. Every table needs a primary key or a replica identity, schema changes are not replicated, sequences must be copied by hand, and the initial copy of a large database takes time and load. It is the right choice when the database is large and the business cannot accept even the minutes pg_upgrade needs.

Major upgrade methods
MethodDowntimeEffortBest for
pg_dump / pg_restoregrows with data sizelowsmall databases, any version jump
pg_upgrade --link or --cloneminutesmediumlarge databases with a short window
logical replication + switchoverseconds to minuteshighlarge databases, near-zero downtime

5The Step After the Upgrade: ANALYZE

The planner chooses query plans using statistics about each column: how many distinct values, the most common values, and histograms. These live in pg_statistic, and in most versions pg_upgrade does not carry them over, nor does a dump and restore. Right after the upgrade, the planner knows almost nothing about the data, and it can choose terrible plans, such as a sequential scan of a large table instead of an index lookup.

So we run ANALYZE straight away. vacuumdb --analyze-in-stages builds rough statistics quickly, then more accurate ones, so the worst plans disappear within minutes while the full pass continues. PostgreSQL 18's pg_upgrade keeps most planner statistics, which shortens this window, but extended statistics still need a fresh ANALYZE; check your version.

Properties
vacuumdb --all --analyze-in-stages
# Generating minimal optimizer statistics (1 target)
# Generating medium optimizer statistics (10 targets)
# Generating default (full) optimizer statistics

6Follow-up questions

Why is the post-upgrade ANALYZE critical for performance?

Because without statistics the planner guesses row counts with default estimates, so it can pick nested loops over millions of rows or sequential scans where an index would take milliseconds. The application may look down even though the database is up. Running vacuumdb --analyze-in-stages right after the upgrade gives the planner usable statistics within minutes.

Which pg_dump version should we use?

The pg_dump from the newer version, 17 here. pg_dump can read older servers, and the newer version knows how to produce output that the new server restores correctly.

Do extensions need attention during an upgrade?

Yes. The new installation must have the same extensions available, which pg_upgrade --check reports, and after the upgrade we run ALTER EXTENSION ... UPDATE where a newer extension version is installed.

7Conclusion

In this answer, we saw that major versions change the on-disk format of the system catalogs, so new binaries refuse an old data directory, while minor versions only need a restart. We compared pg_dump and pg_restore, simple but slow for big databases, pg_upgrade, which reuses data files with --link or --clone for minutes of downtime, and logical replication with a switchover for near-zero downtime at the cost of more moving parts. Finally, we saw that planner statistics must be rebuilt with vacuumdb --analyze-in-stages after most upgrades, while PostgreSQL 18's pg_upgrade keeps most of them.

What to say in the interviewAbout 80 seconds aloud

Open with

Minor versions like 17.1 to 17.2 keep the on-disk format, so I just install the binaries and restart. Major versions change the format of the system catalogs and some internal structures, and the data directory records its version, so PostgreSQL 17 refuses to start on a 15 data directory.

Then, if they want more

There are three main ways. pg_dump and pg_restore, using the newer pg_dump and parallel jobs: simple, works across any versions and removes bloat, but downtime grows with the data size, so it suits small databases. pg_upgrade: it creates the new catalogs, migrates the schema and reuses the data files, and with --link or --clone it takes minutes even for terabytes; I would always run --check first and keep a backup, because with --link the old cluster cannot be started again.

And logical replication to a new 17 cluster, then a short switchover: near-zero downtime, but I need primary keys or replica identity on every table, and I have to handle DDL and sequences myself.

Whichever I choose, I run vacuumdb --analyze-in-stages right after, because planner statistics are not carried over and the planner would otherwise pick bad plans; PostgreSQL 18's pg_upgrade keeps most of them, but I would still check.

About this answer

Last updated
12 November 2026
Versions it names
PostgreSQL 18, PostgreSQL 15, PostgreSQL 17
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
PostgreSQL documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What are the main ways to upgrade PostgreSQL from version 15 to 17, and why can you not...": https://scrivio-interview-library.netlify.app/databases/postgresql/fbe0c1b014/