Skip to content

Instantly share code, notes, and snippets.

@noelruault
Last active June 18, 2025 08:35
Show Gist options
  • Select an option

  • Save noelruault/c62c4113d0d40a4502c1a27f4abff405 to your computer and use it in GitHub Desktop.

Select an option

Save noelruault/c62c4113d0d40a4502c1a27f4abff405 to your computer and use it in GitHub Desktop.
Upgrade PostgreSQL to v14 (Ubuntu)

Postgres upgrade: 10 to 14

1. Backup

First create a backup of all the databases for that (You can continue from B if you dont need a backup)

  1. Log in as postgres user sudo su postgres
  2. Create a backup .sql file for all the data you have in all the databases pg_dumpall > backup.sql

2. Pull repos and install PostgreSQL 14

  1. Download ca-certificates

    sudo apt update
    sudo apt-get install wget ca-certificates
  2. 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'
  3. Update apt and get latest postgres version

    sudo apt update
    sudo apt -y install postgresql=14\* postgresql-client=14\* postgresql-contrib=14\*
  4. (Optional) Check installed versions dpkg -l 'postgres*' | grep ^i

3. Migrate the data (choose one option)

  1. (Option 1: Manual)

    1. Stop postgreSQL sudo systemctl stop postgresql.service

    2. Make sure you are logged in as postgres user sudo su postgres

    3. And you are running the next commands from a directory that is writable by the postgres user like /var/lib/postgresql

      cd /var/lib/postgresql && ls
      #	10  14
    4. Run pg_upgrate to 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'
    5. Update anything required by (like update_extensions.sql)

    6. Switch to regular user exit

    7. 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
  2. (Option 2: Automatic. Use at your own risk 😵‍💫)

    1. 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
    2. 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]

4. Restart and check version

  1. Start the postgresql service sudo systemctl restart postgresql.service
  2. Log in as postgres user sudo su postgres
  3. Check your new postgres version psql -c "SELECT version();"

5. Cleanup

Cleanup up the old version's mess (manual version of pg_dropcluster stated in 5.b)

  1. Option 1 (Manual)

    1. Return as a normal(default user) exit

    2. Uninstall postgres packages if present dpkg -l 'postgres*' | grep ^i

      sudo apt-get remove postgresql-10 postgresql-client-10
    3. Remove the old postgresql directory if present sudo rm -rf /etc/postgresql/10/

    4. Login as postgres user sudo su postgres

    5. 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
  2. Option 2 (Automatic)

    1. Use pg_dropcluster

      sudo 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.

Sources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment