First create a backup of all the databases for that (You can continue from B if you dont need a backup)
- Log in as postgres user
sudo su postgres - Create a backup .sql file for all the data you have in all the databases
pg_dumpall > backup.sql
-
Download ca-certificates
sudo apt update sudo apt-get install wget ca-certificates
-
Add the GPG key and PostgreSQL repository
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'
-
Update apt and get latest postgres version
sudo apt update sudo apt -y install postgresql=14\* postgresql-client=14\* postgresql-contrib=14\*
-
(Optional) Check installed versions
dpkg -l 'postgres*' | grep ^i
-
(Option 1: Manual)
-
Stop postgreSQL
sudo systemctl stop postgresql.service -
Make sure you are logged in as postgres user
sudo su postgres -
And you are running the next commands from a directory that is writable by the postgres user like
/var/lib/postgresqlcd /var/lib/postgresql && ls # 10 14
-
Run
pg_upgrateto migrate the data/usr/lib/postgresql/14/bin/pg_upgrade \ --old-datadir=/var/lib/postgresql/10/main \ --new-datadir=/var/lib/postgresql/14/main \ --old-bindir=/usr/lib/postgresql/10/bin \ --new-bindir=/usr/lib/postgresql/14/bin \ --old-options '-c config_file=/etc/postgresql/10/main/postgresql.conf' \ --new-options '-c config_file=/etc/postgresql/14/main/postgresql.conf' -
Update anything required by (like
update_extensions.sql) -
Switch to regular user
exit -
Swap the ports and delete the old version.
sudo vim /etc/postgresql/14/main/postgresql.conf #change port to 5432 sudo vim /etc/postgresql/10/main/postgresql.conf #change port to 5433
-
-
(Option 2: Automatic. Use at your own risk 😵💫)
-
Delete the PostgreSQL brand new cluster to migrate the old one instead
sudo [pg_dropcluster](http://manpages.ubuntu.com/manpages/trusty/man8/pg_dropcluster.8.html) 14 main --stop # Docs: http://manpages.ubuntu.com/manpages/trusty/man8/pg_dropcluster.8.html -
Upgrade / Migrate
sudo [pg_upgradecluster](http://manpages.ubuntu.com/manpages/trusty/man8/pg_upgradecluster.8.html) -v 14 10 main # Docs: http://manpages.ubuntu.com/manpages/trusty/man8/pg_upgradecluster.8.html # pg_upgradecluster [-v newversion] oldversion name [newdatadir]
-
- Start the postgresql service
sudo systemctl restart postgresql.service - Log in as postgres user
sudo su postgres - Check your new postgres version
psql -c "SELECT version();"
Cleanup up the old version's mess (manual version of pg_dropcluster stated in 5.b)
-
Option 1 (Manual)
-
Return as a normal(default user)
exit -
Uninstall postgres packages if present
dpkg -l 'postgres*' | grep ^isudo apt-get remove postgresql-10 postgresql-client-10
-
Remove the old postgresql directory if present
sudo rm -rf /etc/postgresql/10/ -
Login as postgres user
sudo su postgres -
Go inside psql folder (
/var/lib/postgresql)./delete_old_cluster.sh # by the time this guide was written: equivalent to rm -rf '/var/lib/postgresql/10/main' rm -rf 10 && rm -rf delete_old_cluster.sh
-
-
Option 2 (Automatic)
-
Use
pg_dropclustersudo pg_dropcluster 10 main --stop
-
If everything works well in 2-3, we dont have to apply the backup as we have already migrated the data from the older version to the newer version, the backup is just in case if anything goes wrong.