Upgrading Postgres to newer major version

How to upgrade a Postgres installation to a newer major version, for example from 17 to 18 (Docker and bare-metal).

Ever updated one of your systems and noticed that you aren't satisfying the minimum Postgres version anymore?

I recently upgraded my Paperless-NGX installation (Docker) and found out that I was still on Postgres 15, which is the minimum version supported by Paperless-NGX. At this time, the latest available version of Postgres is 18, and that's also what Paperless-NGX recommends.


💡
WARNING: Be sure to create a backup of your Postgres installation and databases before proceeding with the upgrade!

Simply upgrading the Postgres package (or in my case: upgrading the Docker container) from 15 to 18, or even just 15 to 16, would result in the whole installation breaking.

Instead, the easiest way of upgrading Postgres is dumping the databases and importing them on the new installation or container.

This is fairly easy and straight forward.
First, dump all databases using the following command:

pg_dumpall -U paperless > /var/lib/postgresql/data/backup.sql

The -U flag specifies the database user used to access/dump the databases.
The latter part specifies where to save the dump. In my case, I saved the dump as /var/lib/postgresql/data/backup.sql to the default Postgres path.

Next, shut down the Postgres service (or Container) and upgrade the package or container to your preferred major version (in my case: 18). If using Docker you need to make sure to use a different directory or volume name for the Postgres 18 data. Then copy over the backup.sql to the new Container or server.

Keep in mind that Postgres 18 (at least the Docker container) changed the data path to /var/lib/postgresql/18/docker !

Import the dump on the new container/server using the following command:

psql -U paperless -f /var/lib/postgresql/18/docker/backup.sql

Restart the container or Postgres service, and verify that all your databases are accessible and intact.