Upgrading PostgreSQL 14 to 17 (migration of the data)
The page describes the change of the PostgreSQL version in the images of Mercury DB (HgDB) 3.0 (from 14 to 17) and how to migrate the data of an existing installation.
Why and what changed
Community support of PostgreSQL 14 ends on 2026-11-12. The image of the database (ibpm/postgres:mercury-hgdb-3.0) is based on PostgreSQL 17 (Mercury 3.0.4.20 and later). In the same release the PostgreSQL JDBC driver was updated from 42.3.8 (server) and 42.2.23 (mercury-cmd, Apache ActiveMQ) to 42.7.14. The images that use the driver: the application server, the tool mercury-cmd inside it and Apache ActiveMQ.
The change was verified on PostgreSQL 17.11: the smoke test of the compositions (a single node and two nodes: authentication, REST, heartbeats of the cluster, no errors in the logs), the migration of a database with the application schema, the scripts of the partitioning of the case tables (db/postgresql/01 to 07) and the scenario of the loading of the whole test data set (DatasetScenario).
The data must be migrated
The data directory of PostgreSQL is not compatible between major versions. The image on PostgreSQL 17 started on a data directory created by PostgreSQL 14 does not start (database files are incompatible with server). A new installation needs no action. An existing one must migrate its data before the first start on the new images.
The script bin/postgres-migrate.sh from the repository mercury-docker migrates a directory of data with the dump and restore method:
- it starts a temporary container of the old version on the old directory of data and saves
pg_dumpall(roles with passwords and all databases) and the number of rows of every table, - it moves the old directory aside (
<directory>.pg14-<time>; it stays for the rollback), - it starts a temporary container of the new version (the official
postgresimage, without the initialization scripts of the product) on a new empty directory and restores the dump, - it compares the numbers of rows of all tables in the old and the new database and runs
vacuumdb --all --analyze-in-stages(the dump does not carry the planner statistics).
Procedure
For the compositions of the repository mercury-docker (the volume postgres-db is a directory of the host):
-
Back up the directory of the data (a copy of the directory with the composition stopped is the safest backup) and note the versions of the images in use.
-
Stop the whole composition:
docker compose down. No application may use the database. -
Run the script as a user who can rename the data directory (for example
rooton Linux):./bin/postgres-migrate.sh --data-dir /opt/mercury/postgres-db --backup-dir /opt/mercury/postgres-migrationThe script pulls
postgres:14.24andpostgres:17.11when they are missing (options--old-image,--new-image,--pgdata-subdir). The exit code0means that the restore had no unexpected errors and the numbers of rows of all tables are equal. -
Update the composition to the new images (
docker compose pull, thendocker compose up -d). The composition keeps the same data directory. -
Check the system: a login, a read and a write through REST and
docker compose exec postgres-db psql -U postgres -c "select version()". -
After the retention period delete the old data directory (
<directory>.pg14-<time>) and the dump from the backup directory.
Rollback: stop the composition, delete the new data directory, move the old one back (<directory>.pg14-<time> to <directory>) and start the composition on the images of the old version.
Notes
- The time of the unavailability of the system is the time of the dump and the restore, so it depends on the size of the database. For a database of tens of gigabytes consider
pg_upgrade --link; it needs both versions of the binaries and the script does not cover it. - Schema
publicsince PostgreSQL 15. The rolePUBLICno longer has the privilegeCREATEin the schemapublic; only the owner of the database has it. If the application role (for examplemercury) is not a superuser and does not own the database, the application cannot create tables and fails withpermission denied for schema public. After the migration (and on any PostgreSQL 15+ installation outside the images of the product) grant the privilege in every application database:GRANT CREATE ON SCHEMA public TO mercury;. - Installations without Docker. The package
mercury-cmdreplaces the JDBC driver in the application server, inmercury-cmdand in Apache ActiveMQ with version 42.7.14. When you update a server by hand, replace every olderpostgresql-*.jar(42.2.x, 42.3.x) in these places, so that only one version stays on the classpath. - Lucene indexes and files of the application are not touched by the migration.
- The migration restores the role
mercuryand the other roles with their passwords from the dump, so the passwords of the system do not change. - Kubernetes (the Helm chart): the chart is a new way of delivering the system and it starts on PostgreSQL 17 from the first installation, so there is no data of an older version to migrate. The installation is described in Installing Mercury DB (HgDB) on Kubernetes.
- Connections of the cluster to the database: see Database connection pools and the PostgreSQL connection limit.