Sub topic 5
Preparing for the Upgrade
Assume you know how to manage PostgreSQL clusters with pg_ctl, can work in a Linux shell, and have used pg_dump or pg_basebackup. This chapter prepares an existing PostgreSQL cluster for a major-version upgrade with pg_upgrade. The upgrade itself comes later.
A major upgrade changes the PostgreSQL server version, such as moving from PostgreSQL 16 to PostgreSQL 17. It requires a separate data directory and new server binaries. Do not treat the old data directory as a reusable directory for the new server.
Confirm the Upgrade Plan
Write down the details of both clusters before changing anything:
- The old PostgreSQL version and executable directory.
- The new PostgreSQL version and executable directory.
- The old and new data directories.
- The database server port and operating-system user.
- Installed extensions and their versions.
- Tablespaces and their locations.
- Replication settings and standby servers.
- Available disk space and the planned maintenance window.
The new cluster must be initialized with compatible settings. In particular, check the old cluster's database encoding, locale, and checksum setting. You can inspect database properties with psql:
SELECT datname, pg_encoding_to_char(encoding) AS encoding,
datcollate, datctype
FROM pg_database
WHERE datallowconn
ORDER BY datname;
SHOW data_checksums;
SHOW server_encoding;
SHOW lc_collate;
SHOW lc_ctype;Record the output. The new cluster should use matching encoding and locale values. A mismatch can cause pg_upgrade to stop during its compatibility check.
Read Release Notes and Extension Documentation
Read the PostgreSQL release notes for every major version between the old and new versions. Pay particular attention to:
- Removed or renamed configuration parameters.
- Changes to authentication, replication, and backup behavior.
- Data type or SQL behavior changes.
- Deprecated features that your applications still use.
- Changes required by extensions.
Extensions are installed into a PostgreSQL server installation, not only into a database. Install a version compatible with the new PostgreSQL server before running pg_upgrade. This usually means installing new extension packages and making their control files and shared libraries available under the new installation.
List extensions in every database:
SELECT current_database() AS database_name,
extname,
extversion
FROM pg_extension
ORDER BY extname;Some extensions require a separate upgrade command after the server upgrade. Follow each extension's documentation. Do not assume that installing the new package automatically updates an extension already present in a database.
Protect the Existing Cluster
Create and verify a backup before the maintenance window. A physical backup, such as one made with pg_basebackup, gives you a practical recovery path for the original cluster. Keep the backup separate from both data directories.
A logical backup is also useful for selected databases or objects:
pg_dumpall --globals-only > /backup/pg-globals.sql
pg_dump --format=custom --file=/backup/app.dump appVerify that the files can be read and that the backup location has enough space. If possible, perform a test restore on another server. A backup that has never been restored is not a proven rollback plan.
pg_upgrade does not replace your backup strategy. Its --link mode can make rollback especially restrictive because the old and new clusters share data files.
Check Space, Access, and Dependencies
The operating-system user that owns PostgreSQL must be able to read the old cluster and create files in the new cluster. Check available space for the new cluster, temporary files, logs, and any backup.
Check tablespaces carefully. A tablespace is a directory outside the main data directory that stores database objects. Every tablespace path must exist and be accessible when the new cluster is created.
Also check application connections, scheduled jobs, monitoring agents, and backup tools. Plan to stop or pause anything that might reconnect to the old server during the upgrade.
Build a Preparation Checklist
Use a checklist with an owner and completion time for each item:
| Area | Preparation result |
|---|---|
| Versions | Old and new binaries are installed and identified |
| Backups | Backups exist, are readable, and have a restore plan |
| Extensions | Compatible packages are installed for the new version |
| Settings | Encoding, locale, checksums, ports, and tablespaces are recorded |
| Access | PostgreSQL and administrator users can access required paths |
| Applications | Connections, jobs, and monitoring changes are scheduled |
Exercise
Why should you install compatible extension packages before running pg_upgrade?
Show answer
pg_upgrade must find the extension control files and libraries required by the databases. Missing or incompatible extension files can make the compatibility check fail or leave the upgraded cluster unable to start.