Upgrade postgresql 13 to 14

As a big fan of postgresql I’m following latest releases based on the stable updates channel. But every time a new major release comes our you end up with two running instances on your machine and you have to upgrade your data from the old to the new one. Here some steps how I did it this time:

First take down all services that depend on the database by using systemctl and also bring down the two pg instances:

user@~$ sudo systemctl stop postgresql@13-main
user@~$ sudo systemctl stop postgresql@14-main

Then check the clusters you have:

user@~$ sudo su - postgres
postgres@~$ 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

If you see a new cluster for 14 already check that it’s empty and remove it if so by

postgres@~$ pg_dropcluster --stop 14 main

Then upgrade your existing cluster and bring the new server up again

postgres@~$ pg_upgradecluster 13 main
postgres@~$ exit
user@~$ sudo systemctl stop postgresql@14-main

After this bring your dependent services up and test them. After verifying you can drop the old cluster:

user@~$ sudo su - postgres
postgres@~$ pg_dropcluster --stop 13 main

This should be it. To get rid of the old version you can also run

user@~$ sudo apt purge postgresql-13 postgresql-client-13

The described process worked well for me but be warned that this has potential to delete all your databases. So be careful. Just to be sure I want to state here that I don’t take any responsibility for your data loss or the resulting problems of it ;-) Having backups before starting this process would be a good idea!