This guide upgrades a dedicated Rocky Linux 9.2 server from PostgreSQL 16.x to
17.x in place with pg_upgrade. PostgreSQL 17 is installed alongside PostgreSQL
16, and the old data directory is retained for rollback.
The commands use --copy, not --link. Copy mode requires enough free disk
space for another copy of the cluster, but PostgreSQL 16 remains usable until
PostgreSQL 17 receives new production writes.
The paths below match installations made with the official PostgreSQL Yum
repository:
Set the PostgreSQL upgrade paths
exportOLD_BIN=/usr/pgsql-16/bin
exportNEW_BIN=/usr/pgsql-17/bin
exportOLD_DATA=/var/lib/pgsql/16/data
exportNEW_DATA=/var/lib/pgsql/17/data
exportUPGRADE_DIR=/var/lib/pgsql/upgrade-16-to-17
Adjust them if your current server uses different paths.
1. Inventory the PostgreSQL 16 cluster
Confirm the running version and data directory:
Check the PostgreSQL 16 version and data directory
Check tablespaces because their data also needs space during a copy-mode
upgrade:
List PostgreSQL tablespaces
sudo-upostgres"$OLD_BIN/psql"-X-c"
SELECT spcname, pg_tablespace_location(oid)
FROM pg_tablespace
ORDER BY spcname;
"
List extensions in every connectable database:
List extensions in every PostgreSQL database
sudo-iupostgresbash<<'EOF'
while IFS= read -r database; do
echo "=== Database: $database ==="
/usr/pgsql-16/bin/psql -X -d "$database" -c \
"SELECT extname, extversion FROM pg_extension ORDER BY extname;"
done < <(
/usr/pgsql-16/bin/psql -XAtc \
"SELECT datname FROM pg_database WHERE datallowconn AND NOT datistemplate"
)
EOF
Install a PostgreSQL 17-compatible build of every extension containing native
code before running pg_upgrade. Do not manually create those extensions in the
empty PostgreSQL 17 cluster; pg_upgrade migrates their definitions.
2. Update PostgreSQL 16 to its latest minor release
First update the existing 16.x packages. Plan a short outage for this step.
Check disk space for the data directory and every user tablespace:
Check data size and available disk space
sudodu-sh"$OLD_DATA"
sudodf-h"$OLD_DATA"
A filesystem or VM snapshot taken while PostgreSQL is stopped provides an
additional rollback layer.
4. Install PostgreSQL 17 and extensions
Install PostgreSQL 17 alongside PostgreSQL 16:
Install PostgreSQL 17 packages
sudodnfinstall-y\
postgresql17-server\
postgresql17-contrib\
postgresql17-devel
If pgvector was installed from the PostgreSQL Yum repository, install its
PostgreSQL 17 package:
Install pgvector for PostgreSQL 17
sudodnfinstall-ypgvector_17
Prefer the RPM package when available and do not install pgvector with both RPM
and PGXN. If an extension is available only through PGXN, select the PostgreSQL
17 pg_config explicitly:
Install a PGXN extension for PostgreSQL 17
sudopgxnclientinstall\
--pg_config/usr/pgsql-17/bin/pg_config\
vector
Confirm that both versions are installed:
Check the installed PostgreSQL binaries
"$OLD_BIN/postgres"--version
"$NEW_BIN/postgres"--version
"$NEW_BIN/pg_upgrade"--version
5. Initialize an empty PostgreSQL 17 cluster
Initialize the new cluster with the encoding and locale recorded earlier. Based
on the original Rocky Linux installation, these are likely UTF8 and
en_US.UTF-8:
Initialize the PostgreSQL 17 cluster
sudoinstall-d-opostgres-gpostgres-m700"$NEW_DATA"
sudo-upostgres"$NEW_BIN/initdb"\
--pgdata="$NEW_DATA"\
--encoding=UTF8\
--locale=en_US.UTF-8
If pg_controldata reported checksum version 1, remove the newly initialized
empty directory and initialize it with checksums instead:
Initialize PostgreSQL 17 with data checksums
sudorm-rf"$NEW_DATA"
sudoinstall-d-opostgres-gpostgres-m700"$NEW_DATA"
sudo-upostgres"$NEW_BIN/initdb"\
--pgdata="$NEW_DATA"\
--encoding=UTF8\
--locale=en_US.UTF-8\
--data-checksums
Only remove NEW_DATA at this point, while it is a newly initialized empty
cluster. Do not start PostgreSQL 17 yet.
For example, an error about not loading $libdir/vector means the PostgreSQL 17
pgvector library is missing. Install pgvector_17, then rerun the check.
8. Run the PostgreSQL major upgrade
Run the real upgrade in copy mode:
Upgrade PostgreSQL 16 to PostgreSQL 17
sudo-iupostgresbash<<'EOF'
cd /var/lib/pgsql/upgrade-16-to-17
/usr/pgsql-17/bin/pg_upgrade \
--old-bindir=/usr/pgsql-16/bin \
--new-bindir=/usr/pgsql-17/bin \
--old-datadir=/var/lib/pgsql/16/data \
--new-datadir=/var/lib/pgsql/17/data \
--username=postgres \
--jobs="$(nproc)" \
--copy
EOF
Do not use --no-sync on a production server. Read the complete output and run
any required extension-update or index-rebuild scripts exactly as printed. Keep
the generated old-cluster deletion script, but do not run it yet.
9. Migrate configuration
Do not overwrite PostgreSQL 17's entire postgresql.conf with the PostgreSQL 16
file. Compare the files and manually reapply custom settings that remain valid:
Compare PostgreSQL 16 and 17 configuration files
sudodiff-u\
"$NEW_DATA/postgresql.conf"\
"$OLD_DATA/postgresql.conf"\
|less
Review settings such as listen_addresses, port, memory limits, WAL settings,
logging, SSL, and shared_preload_libraries. Any library named in
shared_preload_libraries must also be installed for PostgreSQL 17.
The client authentication files can normally be copied after review:
Switch the enabled service and start PostgreSQL 17:
Enable and start PostgreSQL 17
sudosystemctldisablepostgresql-16
sudosystemctlenablepostgresql-17
sudosystemctlstartpostgresql-17
sudosystemctlstatuspostgresql-17--no-pager
Verify the server identity, data directory, encoding, and locale:
Verify the upgraded PostgreSQL 17 cluster
sudo-upostgres"$NEW_BIN/psql"-X-c"
SELECT version();
SHOW data_directory;
SHOW server_encoding;
SHOW lc_collate;
SHOW lc_ctype;
"
sudo-upostgres"$NEW_BIN/pg_isready"
Check that all databases are present:
List upgraded PostgreSQL databases and sizes
sudo-upostgres"$NEW_BIN/psql"-X-c"
SELECT datname, pg_size_pretty(pg_database_size(datname)) AS size
FROM pg_database
WHERE datallowconn
ORDER BY datname;
"
11. Update extensions
First run any extension-update script produced by pg_upgrade. Then list the
installed extension versions:
Check extensions after the PostgreSQL upgrade
sudo-iupostgresbash<<'EOF'
while IFS= read -r database; do
echo "=== Database: $database ==="
/usr/pgsql-17/bin/psql -X -d "$database" -c \
"SELECT extname, extversion FROM pg_extension ORDER BY extname;"
done < <(
/usr/pgsql-17/bin/psql -XAtc \
"SELECT datname FROM pg_database WHERE datallowconn AND NOT datistemplate"
)
EOF
Update pgvector in each database that uses it:
Update and verify the pgvector extension
sudo-upostgres"$NEW_BIN/psql"\
--dbname=your_database\
--command="ALTER EXTENSION vector UPDATE;"
sudo-upostgres"$NEW_BIN/psql"\
--dbname=your_database\
--command="SELECT extversion FROM pg_extension WHERE extname = 'vector';"
Update other extensions only where they are installed:
Update PostgreSQL contrib extensions
ALTEREXTENSIONpg_trgmUPDATE;
ALTER EXTENSION btree_gist UPDATE;
12. Regenerate optimizer statistics
pg_upgrade does not transfer optimizer statistics. Regenerate them in stages
before returning the server to normal traffic:
Regenerate PostgreSQL optimizer statistics in stages
sudo-upostgres"$NEW_BIN/vacuumdb"\
--all\
--analyze-in-stages\
--jobs="$(nproc)"
After normal operation resumes, run a complete analyze:
Run a complete PostgreSQL analyze
sudo-upostgres"$NEW_BIN/vacuumdb"\
--all\
--analyze-only\
--jobs="$(nproc)"
13. Validate before deleting PostgreSQL 16
Review the service log:
Check recent PostgreSQL 17 service logs
sudojournalctl\
-upostgresql-17\
--since"1 hour ago"\
--no-pager
Check for invalid indexes in each important database:
Find invalid PostgreSQL indexes
sudo-upostgres"$NEW_BIN/psql"-X-dyour_database-c"
SELECT n.nspname AS schema_name, c.relname AS index_name
FROM pg_index i
JOIN pg_class c ON c.oid = i.indexrelid
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE NOT i.indisvalid
ORDER BY 1, 2;
"
Validate database and table counts, important row counts, application login,
reads and writes, scheduled jobs, backups, monitoring, connection pools, and
pgvector searches and indexes. Keep the PostgreSQL 16 packages and
/var/lib/pgsql/16/data for an agreed retention period.
Roll back before production writes
Because the upgrade used --copy, PostgreSQL 16 remains unchanged. Before
PostgreSQL 17 receives production writes, rollback is straightforward:
Roll back to PostgreSQL 16 before new writes
sudosystemctlstoppostgresql-17
sudosystemctldisablepostgresql-17
sudosystemctlenablepostgresql-16
sudosystemctlstartpostgresql-16
sudosystemctlstatuspostgresql-16--no-pager
After clients write to PostgreSQL 17, restarting PostgreSQL 16 would discard
those new writes. Stop all clients and plan how to export or reconcile the
PostgreSQL 17 changes before attempting rollback.
Final cleanup
Only after PostgreSQL 17 has passed validation and fresh backups are working,
disable PostgreSQL 16 permanently:
Disable the old PostgreSQL 16 service
sudosystemctldisablepostgresql-16
After the retention period, remove the old packages. Review the DNF transaction
before accepting it so that libraries required by other software are not
removed:
Remove the old PostgreSQL 16 packages
sudodnfremove\
postgresql16-server\
postgresql16-contrib\
postgresql16-devel
Finally, delete the old cluster only when you are certain it is no longer needed.
Use the deletion script generated by pg_upgrade, or archive the old data
directory according to your backup and retention policy.