Sub topic 12
Troubleshooting Common `pg_upgrade` Failures
Assume you know basic PostgreSQL administration, can use the Linux command line, and understand clusters, data directories, extensions, backups, and streaming replication. This chapter focuses on failures during compatibility checks, the upgrade itself, and the first startup of the new cluster.
Start with the Check Log
Run pg_upgrade --check before the maintenance window. The command does not change either cluster. It writes detailed logs to the directory where you run it.
/opt/pg17/bin/pg_upgrade \
--old-bindir=/opt/pg16/bin \
--new-bindir=/opt/pg17/bin \
--old-datadir=/data/pg16 \
--new-datadir=/data/pg17 \
--check 2>&1 | tee /tmp/pg_upgrade-check.logRead the final error first, then inspect the referenced log file. A message near the end often summarizes a problem whose details appear earlier.
grep -E "database|extension|tablespace|permission|fatal|error" \
/tmp/pg_upgrade-check.logFix one reported cause at a time, then run the check again. Do not start the real upgrade until check mode succeeds.
The Old Cluster Is Still Running
A common failure says that the old server is running or that a database connection exists. pg_upgrade needs exclusive access to both clusters.
Check the status of each cluster:
/opt/pg16/bin/pg_ctl -D /data/pg16 status
/opt/pg17/bin/pg_ctl -D /data/pg17 statusStop both clusters before checking or upgrading:
/opt/pg16/bin/pg_ctl -D /data/pg16 -m fast stop
/opt/pg17/bin/pg_ctl -D /data/pg17 -m fast stopIf a server refuses to stop, find the remaining PostgreSQL processes and identify the cluster they use. Do not kill an unknown process. A second PostgreSQL instance, an automated health check, or a service manager may restart the server immediately.
Binaries, Versions, and Paths Do Not Match
pg_upgrade must use the old server's binaries with the old data directory and the new server's binaries with the new data directory. Accidentally reversing these paths produces confusing errors.
Confirm each binary's version:
/opt/pg16/bin/postgres --version
/opt/pg17/bin/postgres --versionAlso verify that the data directories belong to the expected versions:
cat /data/pg16/PG_VERSION
cat /data/pg17/PG_VERSIONThe new directory must be initialized by the new version. Do not point --new-datadir at the old data directory.
Extensions Are Missing or Incompatible
An extension is PostgreSQL software installed outside the core server. The extension's control file and shared library must exist in the new installation before the upgrade.
For example, if the old cluster uses postgis, install the matching PostgreSQL 17 package before running pg_upgrade. Then compare installed extensions in each cluster:
SELECT extname, extversion
FROM pg_extension
ORDER BY extname;Run this query with psql against both clusters. Upgrade or replace extensions according to their documentation. A matching SQL extension name is not enough if the required shared library is absent.
Permissions, Tablespaces, and File Transfer Errors
The operating-system user running pg_upgrade must be able to read the old cluster and write the new cluster. The PostgreSQL server user must also own the new data directory and its tablespaces.
| Symptom | Likely cause | Check |
|---|---|---|
| `permission denied` | Wrong owner or directory mode | `ls -ld /data/pg16 /data/pg17` |
| Tablespace path failure | Missing or inaccessible mount point | `findmnt` and `ls -ld` |
| Link or clone failure | Filesystems do not support the selected mode | Test the paths and filesystem type |
Copy mode is slower but works across more filesystems. Clone mode needs filesystem support. Link mode is fastest, but it removes the normal independent rollback path.
Startup and Post-Upgrade Failures
A successful upgrade can still fail at startup because the new postgresql.conf, authentication rules, port, or extension settings are wrong. Start the new cluster with a log file and read that log immediately:
/opt/pg17/bin/pg_ctl \
-D /data/pg17 \
-l /data/pg17/server.log \
start
tail -n 50 /data/pg17/server.logIf the server starts but applications fail, compare pg_hba.conf, connection settings, roles, and extension availability. Rebuild planner statistics before judging query performance:
/opt/pg17/bin/vacuumdb --all --analyze-in-stagesA Repeatable Diagnosis Path
Exercise
The check reports that an extension's shared library is missing from the new installation. Should you continue the upgrade and install it later?
Show answer
No. Install the extension package for the new PostgreSQL version, verify that the library and control file are available, and rerun pg_upgrade --check.