In-place PostgreSQL upgrade on Debian
Before you begin
Check the clusters and their ports with pg_lsclusters. Make sure to create a
backup for all databases! You can use pg_dumpall -p <port> command for
that.
$ sudo su - postgres
$ export BACKUP_DIR="/tmp/pgbackup"
$ mkdir -p "${BACKUP_DIR}"
$ pg_lsclusters -j \
| jq -r '.[]|[.config.cluster_name, .config.port] | @tsv' \
| while IFS=$'\t' read -r cluster port; do \
pg_dumpall -p "${port}" > "${BACKUP_DIR}/dump_${cluster//\//_}_$( date +"%Y-%m-%dT%H:%M:%S%z" ).sql"; \
done
Upgrading
To upgrade a cluster to a newer PostgreSQL major version run the following command - in the following example we’re upgrading from version 17 to 18.
$ pg_upgradecluster 17 main -v 18
Before upgrading to the newest version you might need to drop the cluster which is already running with the desired PostgreSQL version.
$ pg_dropcluster --stop 18 main
Make sure that the upgraded cluster works as expected and then drop the old one.
$ pg_dropcluster 17 main
Once done - remove old postgres-* packages.
References
/usr/share/doc/postgresql-common/README.Debian.gz