Upgrading Ubuntu and the PostgreSQL database

Need to upgrade your Ubuntu and PostgreSQL versions? Follow these steps, it's not as scary and you might think...

In my role as a system administrator, I get tasked with upgrading ours or clients servers, the latest batch of upgrades involved bringing an Ubuntu 18.04 server running PostgreSQL 10, up to Ubuntu 24.04 and PostgreSQL 16.

The actual Ubuntu upgrades were simple, just run apt to upgrade the packages, reboot, and then run the do-release-upgrade tool and follow the prompts.

However, I could not go straight to Ubuntu 18.04 to Ubuntu 24.04, I had to follow the LTS releases, so, 18.04 -> 20.04 -> 22.04 -> 24.04, no problem, just rinse and repeat the upgrade steps.

But, with each upgrade, came a newer PostgreSQL version with the increased risk of support for the older one dropped, so in between each Ubuntu upgrade, I also upgraded and migrated the old database to the latest version supported by the Ubuntu release.

Here's what I did to achieve this.

IMPORTANT
Before proceeding it is highly recommended that you perform a backup or snapshot of your system!

I will not accept responsibility for any data loss you may incur when following these steps, this article is for information only.

Ok, so say we are starting at Ubuntu 18.04, once you have performed your backup, (seriously, do a backup!), we can run the following apt commands to update the current software on the system.

apt update
apt list --upgradable
apt upgrade
apt autoremove -y
apt clean

It's best to reboot to ensure everything is applied correctly.

reboot

Once the system has rebooted, we can now run the upgrade command, it will prompt you to confirm a few things so read the prompts carefully and respond with the appropriate response.

do-release-upgrade

The upgrade might take a while so grab a cup of tea and keep an eye on it, once it is finished it will as you to reboot, so do this and then log back in, you should now be on Ubuntu 20.04.

Because PostgreSQL 10 will still be installed we need to install PostgreSQL 12, we can do this with the following command, we do not need to specify the version because in Ubuntu 20.04, PostgreSQL 12 is the default.

apt install postgresql

Once it has installed, we can upgrade our database from PostgreSQL 10 to 12, to do this we will stop PostgreSQL, remove the version 12 cluster (temporarily) and run an upgrade command against our version 10 database.

Important Note
The upgrade will copy the data in the old database to the new one, this means you should ensure you have enough free disk space to essentially hold a duplicate of your current database.

So, if your database is 20GB in size, you will need another 20GB at least of free space.
systemctl stop postgresql
pg_lsclusters
pg_dropcluster 12 main --stop
pg_upgradecluster 10 main

The upgrade command might take a while depending on the size of your database, but once done, you should have an upgraded version containing your existing data.

Confirm everything still works as expected, if it is, you can remove the old version of PostgreSQL, the following command should remove the packages, directories and files associated with PostgreSQL 10.

apt remove --purge postgresql-10 postgresql-client-10

Then, to upgrade to the next version, Ubuntu 22.04 and PostgreSQL 14, rerun these steps and just tweak the version numbers, and finally the same when going to Ubuntu 24.04 and PostgreSQL 16.

Enjoyed this post?

If so, please consider buying me a chilled pint to sip while writing the next one!

Gift a pint

Subscribe to Sam::blog()

Don’t miss out on the latest posts. Sign up now to get access to the library of members-only posts.
[email protected]
Subscribe