It’s easy. I thought it would be hard.
First lets install the postgres tooling for ubuntu
sudo apt install -y postgresql-common
Then lets add and enable the PostgreSQL APT repository
sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
Install the latest postgresql
apt install postgresql
see all clusters
pg_lsclusters
dpkg --get-selections | grep postgres
there will be new cluster. rename it to main_pristine
pg_renamecluster 17 main main_pristine
Choose which cluster to upgrade
sudo pg_upgradecluster 13 main
Because pg_upgradecluster comes from the new package it will magically creates a new version near the old version and you will have something like :
# pg_lsclusters
Ver Cluster Port Status Owner Data directory Log file
13 main 5432 online postgres /var/lib/postgresql/13/main /var/log/postgresql/postgresql-13-main.log
17 main_pristine 5434 online postgres /var/lib/postgresql/17/main_pristine /var/log/postgresql/postgresql-17-main_pristine.log
# sudo pg_upgradecluster 13 main
.... a lot of upgrading
# pg_lsclusters
Ver Cluster Port Status Owner Data directory Log file
13 main 5435 down postgres /var/lib/postgresql/13/main /var/log/postgresql/postgresql-13-main.log
17 main 5432 online postgres /var/lib/postgresql/17/main /var/log/postgresql/postgresql-17-main.log
17 main_pristine 5434 online postgres /var/lib/postgresql/17/main_pristine /var/log/postgresql/postgresql-17-main_pristine.log
Notice that the port and everything is in place! Nice!
Finally lets do some cleanup.
pg_dropcluster 13 main --stop
pg_dropcluster 17 main_pristine --stop
This article would be not possible without
https://gorails.com/guides/upgrading-postgresql-version-on-ubuntu-server
https://www.directedignorance.com/blog/upgrading-postgresql-14-to-16-on-ubuntu