How to Upgrade PostgreSQL Version on Ubuntu Server
When you need to upgrade the PostgreSQL version on Ubuntu, the process is pretty straightforward. This works for upgrading any version from Postgres 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 10, 11, 12, 13, 14, 15, 16, 17
Overview
We're going to be upgrading PostgreSQL server on Ubuntu in this guide. It doesn't matter which version you're upgrading from or to. You can do this with Postgres 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 10, 11, 12, 13, 14, 15, 16, 17 or whatever is the most recent version.
In this example, I'm upgrading Postgres 14 to Postgres 15 but all you have to do is replace the version numbers in the commands below to match which old version you're using and the new version you're upgrading to.
This only takes a couple minutes if you have a small database, so let's get started!
1. Install the latest version of Postgres
If you're using the default version available on Ubuntu, you can just upgrade to the latest postgres by running the following:
sudo apt-get upgrade
Otherwise if you want to upgrade to the very latest Postgres version, you can follow the instructions on their website here: https://www.postgresql.org/download/linux/ubuntu/
To find the installed versions that you currently have on your machine, you can run the following:
$ dpkg --get-selections | grep postgres
postgresql-14 install
postgresql-15 install
postgresql-client install
postgresql-client-14 install
postgresql-client-15 install
postgresql-client-common install
postgresql-common install
You can also list the clusters that are on your machine by running
$ pg_lsclusters
Ver Cluster Port Status Owner Data directory Log file
14 main 5432 online postgres /var/lib/postgresql/14/main /var/log/postgresql/postgresql-14-main.log
15 main 5433 online postgres /var/lib/postgresql/15/main /var/log/postgresql/postgresql-15-main.log
2. Stop Postgres before we make any changes
First thing's first, we need to stop any services using postgres so we can safely migrate our database.
sudo service postgresql stop
3. Rename the new Postgres version's default cluster
When Postgres packages install, they create a default cluster for you to use. We need to rename the new postgres cluster so that when we upgrade the old cluster the names won't conflict.
sudo pg_renamecluster 15 main main_pristine
4. Upgrade the old cluster to the latest version
Replace the version (14) here with the old version of Postgres that you're currently using.
sudo pg_upgradecluster 14 main
5. Make sure everything is working again
We can start Postgres back up again and this time it should be running the new postgres 15 cluster.
sudo service postgresql start
You should also see that the old cluster is down and the new version of Postgres is up:
$ pg_lsclusters
Ver Cluster Port Status Owner Data directory Log file
14 main 5434 down postgres /var/lib/postgresql/14/main /var/log/postgresql/postgresql-14-main.log
15 main 5432 online postgres /var/lib/postgresql/15/main /var/log/postgresql/postgresql-15-main.log
15 main_pristine 5433 online postgres /var/lib/postgresql/15/main_pristine /var/log/postgresql/postgresql-15-main_pristine.log
6. Drop the old cluster
Optionally, you can drop the old cluster once you've verified the new one works and you don't need the old cluster anymore.
sudo pg_dropcluster 14 main --stop
You can also drop the pristine database from the newer version as well.
sudo pg_dropcluster 15 main_pristine --stop