The Linux Page

Upgrade PostgreSQL when upgrading Ubuntu

Upgrading PostgreSQL between OS upgrades

The following probably works with other versions of Linux, but I ran those commands on Ubuntu, so if you have a different OS, you may want to verify each step closely first.

The idea when you upgrade to a new OS is that the new version is available in that OS and the old version is now obsolete, so the best is to upgrade to the new database and make sure that the new database work. Then you can get rid of the old cluster.

The following are the steps I used:

sudo su -
su - postgresql
pg_dropcluster --stop 12 main
pg_upgradecluster 10 main

Once the transfer is done AND THE NEW DATABASE WORKS AS EXPECTED, we can then destroy the old cluster by purging the old version:

sudo apt-get purge postgresql-10 postgresql-client-10


As noted in the steps, the version I'm upgrading to is 12 from version 10. This is from Ubuntu 18.04 to Ubuntu 20.04. Please make sure to use the correct versions. You can see the versions using the dpkg -l command:

$ dpkg -l | grep postgresql
ii  postgresql                             12+214ubuntu0.1                   all          object-relational SQL database (supported version)
ii  postgresql-10                          10.17-0ubuntu0.18.04.1            amd64        object-relational SQL database, version 10 server
ii  postgresql-12                          12.7-0ubuntu0.20.04.1             amd64        object-relational SQL database, version 12 server
ii  postgresql-client-10                   10.17-0ubuntu0.18.04.1            amd64        front-end programs for PostgreSQL 10
ii  postgresql-client-12                   12.7-0ubuntu0.20.04.1             amd64        front-end programs for PostgreSQL 12
ii  postgresql-client-common               214ubuntu0.1                      all          manager for multiple PostgreSQL client versions
ii  postgresql-common                      214ubuntu0.1                      all          PostgreSQL database-cluster manager

Here we can see that we have version 10 and 12.