VPS Snaps

How to upgrade PostgreSQL 18 to 19 in Docker Compose without losing data

PostgreSQL 19 was released on November 19, 2026, and the official postgres:19 image is on Docker Hub. You cannot move to it by changing the tag: a new major version cannot read an 18 data directory, and the image keeps each major version's data in its own folder, so a tag bump starts an empty database. This guide runs a postgres:19 container beside your 18 one, copies every database and role across with the version 19 pg_dumpall, checks the copy, then switches your app. The 18 volume is never touched, so going back is one command. The commands are for Docker Compose v2 (docker compose) and the official postgres images.

Why changing the tag gives you an empty database

From PostgreSQL 18 the official image sets PGDATA per major version and mounts the volume one level up, at /var/lib/postgresql. Version 18 keeps its files in /var/lib/postgresql/18/docker. Version 19 looks in /var/lib/postgresql/19/docker.

If you change image: postgres:18 to postgres:19 and keep the same volume, the 19 container finds an empty 19/docker folder, runs initdb, and starts a brand new database with only the user and database from your environment variables. Your 18 data is still in the volume, but your app now connects to an empty server.

If you already changed the tag and the app came up empty, stop the app before anyone signs up or writes data. Put the tag back to postgres:18 and your data is there again. Then follow this guide.

PostgreSQL 19 also changes a few things that can affect an app moving from 18. These come from the 19 release notes:

Change in 19What to check
standard_conforming_strings is always onDumps made by an older pg_dump with it off will not load properly. This guide dumps with the 19 tools, which avoids that.
A warning is logged after every MD5 password loginRe-set any MD5 password so it is stored as SCRAM. The check is below.
RADIUS authentication removedRemove any radius lines from a custom pg_hba.conf.
JIT is off by defaultMost apps are unaffected. If you relied on JIT for heavy analytics queries, set jit = on and compare.
max_locks_per_transaction default doubled to 128If you set it yourself, the release notes say you must double your value to keep the same capacity.
io_workers removedReplaced by io_min_workers and io_max_workers. Delete io_workers from any custom config file before you start 19 with it.
Full text search and pg_trgm follow the database's locale providerOnly matters for databases using the ICU or builtin provider. See the check below.

Back up and record what you have

Take a snapshot of the server at your provider before you change anything, and keep a copy of the Compose file. The examples assume the database service is called db, its volume is pgdata, and your app service is <app-service>. Replace <db-user> with the value of POSTGRES_USER and <db-name> with your database name.

Terminal
cp docker-compose.yml docker-compose.yml.pg18
docker compose exec db psql -U <db-user> -d postgres -c 'SELECT version();'
docker compose exec db psql -U <db-user> -d postgres -c '\l'
docker compose exec db psql -U <db-user> -d postgres -c 'SELECT datname, datlocprovider FROM pg_database;'
docker compose exec db psql -U <db-user> -d <db-name> -c '\dx'
docker compose exec db psql -U <db-user> -d <db-name> -c 'SELECT count(*) FROM <important-table>;'

Write down the row counts of two or three tables that matter. The \dx line lists extensions: if you use one that is not in the official image, such as PostGIS or pgvector, you need that project's image built for PostgreSQL 19 instead of postgres:19. Check it exists before you start.

In the datlocprovider column, c means libc, which is the image's default. If a database shows i (ICU) or b (builtin) and it stores tsvector values in a column, note that full text search parsing now follows that provider and can give different output. The 19 release notes recommend recomputing stored tsvector values for such databases.

Add a postgres:19 service beside the old one

Add a second service with the same credentials and a new, empty volume. Leave db exactly as it is. Both images mount at /var/lib/postgresql.

docker-compose.yml
services:
  db:
    image: postgres:18
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    volumes:
      - pgdata:/var/lib/postgresql
  db19:
    image: postgres:19
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    volumes:
      - pgdata19:/var/lib/postgresql
volumes:
  pgdata:
  pgdata19:
Terminal
docker compose up -d db19
docker compose exec db19 pg_isready -U <db-user>

Wait until pg_isready reports accepting connections. The image only creates the user and database from the environment variables because the new volume is empty.

A separate volume costs some disk space for a while, but it means the 18 data is never written to during the upgrade, and you can delete it with one command once you are sure.

Stop writes and copy everything across

Stop the app so nothing changes while you copy. Then dump the 18 server with the pg_dumpall inside db19. The PostgreSQL docs recommend dumping with the newer version's tools, and the 19 release notes say dumps for 19 should be made with 19's pg_dump or pg_dumpall. pg_dumpall includes every database plus roles and their passwords, which a single-database pg_dump leaves out.

Terminal
docker compose stop <app-service>
docker compose exec -T db19 sh -c 'PGPASSWORD="$POSTGRES_PASSWORD" pg_dumpall -h db -U "$POSTGRES_USER"' > pg18-dumpall.sql
chmod 600 pg18-dumpall.sql
ls -lh pg18-dumpall.sql
tail -n 3 pg18-dumpall.sql

The single quotes make the variables expand inside the container, where POSTGRES_USER and POSTGRES_PASSWORD are set. A complete dump ends with PostgreSQL database cluster dump complete. If you use POSTGRES_PASSWORD_FILE, replace $POSTGRES_PASSWORD with $(cat "$POSTGRES_PASSWORD_FILE").

Now load it into PostgreSQL 19. Connections over the Unix socket inside the container do not need a password.

Terminal
docker compose exec -T db19 sh -c 'psql -X -U "$POSTGRES_USER" -d postgres' < pg18-dumpall.sql 2> restore-errors.log
grep ERROR restore-errors.log

Expect two errors at most: role "<db-user>" already exists and database "<db-name>" already exists, because the image created both when db19 started. The pg_dumpall docs call the role error harmless. Any other error, such as a missing extension, must be fixed before you switch.

The dump holds every role's password hash. Keep it readable only by you, copy it off the server with your other backups, and delete stray copies when you are done.

Check the new database before you switch

Terminal
docker compose exec db19 psql -U <db-user> -d postgres -c 'SELECT version();'
docker compose exec db19 psql -U <db-user> -d postgres -c '\l'
docker compose exec db19 psql -U <db-user> -d <db-name> -c 'SELECT count(*) FROM <important-table>;'
docker compose exec db19 sh -c 'vacuumdb -U "$POSTGRES_USER" -a -z'

The row counts should match your notes. vacuumdb -a -z vacuums and analyzes every database, so the planner has statistics from the first query.

Then look for passwords still stored as MD5 hashes. MD5 passwords were deprecated in 18, and 19 logs a warning on every MD5 login. The image uses scram-sha-256 for network connections, and a role with an MD5 hash cannot log in that way.

Terminal
docker compose exec db19 psql -U <db-user> -d postgres -c "SELECT rolname FROM pg_authid WHERE rolpassword LIKE 'md5%';"

If any roles are listed, set their passwords again so they are stored as SCRAM:

Terminal
docker compose exec db19 psql -U <db-user> -d postgres -c "ALTER ROLE <role-name> WITH PASSWORD '<password>';"

Switch the app to PostgreSQL 19

Stop both database containers. Edit the Compose file: change db to image: postgres:19, change its volume line to pgdata19:/var/lib/postgresql, and delete the db19 service. Keep both volumes listed under volumes:.

docker-compose.yml
services:
  db:
    image: postgres:19
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    volumes:
      - pgdata19:/var/lib/postgresql
volumes:
  pgdata:
  pgdata19:
Terminal
docker compose stop db db19
docker compose up -d --remove-orphans
docker compose exec db psql -U <db-user> -d postgres -c 'SELECT version();'
docker compose logs --tail=50 <app-service>

--remove-orphans removes the old db19 container. Its data stays in the pgdata19 volume, which db now uses. The app still connects to the host name db, so its settings do not change. Click through the parts of the app that write data before you call it done.

Never run docker compose down -v on this project. The -v flag deletes named volumes, including pgdata, which holds your PostgreSQL 18 data and is your way back.

Roll back if something is wrong

The 18 volume is exactly as it was when you stopped the app. To go back, restore the old Compose file and recreate the containers:

Terminal
cp docker-compose.yml.pg18 docker-compose.yml
docker compose up -d --remove-orphans

Anything written to 19 after the switch is not in the 18 volume. Decide early, before much new data builds up. Once you are sure, and only after you have a good backup from 19, remove the old volume. Compose prefixes volume names with the project name, so find the full name first.

Terminal
docker volume ls
docker volume rm <project>_pgdata

Schedule backups on PostgreSQL 19

Set up the nightly dump straight away. This crontab line dumps one database, compresses it and dates the file. In crontab, % must be written as \%.

crontab -e
0 2 * * * cd /opt/<app> && docker compose exec -T db sh -c 'pg_dump -U "$POSTGRES_USER" -d <db-name>' | gzip > /var/backups/<db-name>-$(date +\%F).sql.gz

A pg_dump file does not include roles. Save them now and then with pg_dumpall --globals-only, and copy both files off the server. The Docker Compose database backup guide and database backups to S3 cover rotation and offsite copies.

Prove the backup restores

Restore last night's dump into a throwaway postgres:19 container, count rows, and remove it. This proves the file is complete and loads on the version you now run.

Terminal
docker run -d --name pg-restore-test -e POSTGRES_PASSWORD=<test-password> postgres:19
sleep 10
docker exec pg-restore-test createuser -U postgres <db-user>
docker exec pg-restore-test createdb -U postgres -O <db-user> restore_test
gunzip -c /var/backups/<db-name>-<YYYY-MM-DD>.sql.gz | docker exec -i pg-restore-test psql -X -U postgres -d restore_test
docker exec pg-restore-test psql -U postgres -d restore_test -c 'SELECT count(*) FROM <important-table>;'
docker rm -f -v pg-restore-test

Creating the role first lets the ownership statements in the dump succeed. docker rm -f -v removes the container and its anonymous data volume. More ways to check a restore are in how to test a backup restore.

Frequently asked questions

Should I upgrade to PostgreSQL 19 now or wait?
PostgreSQL 18 keeps getting fixes for years, so there is no deadline. Many people wait for the first minor release and for any extension images they use to publish 19 builds. The steps here are the same whenever you do it.
Can I use pg_upgrade instead of a dump?
Yes, and it is faster for large databases, but the postgres:19 image does not contain the 18 binaries that pg_upgrade needs, so you need a separate upgrade image. The 19 release notes also list cases pg_upgrade refuses, such as btree_gist indexes on inet or cidr columns. A dump and restore is simpler in Compose and leaves the old data untouched.
Why does the restore say role already exists?
The image creates POSTGRES_USER when the new container starts, and the dump tries to create it again. The pg_dumpall docs say this error is harmless and should be ignored.
Do I need to reindex after a dump and restore?
No. Loading a dump builds every index fresh on 19. The reindex advice in the release notes is for clusters moved with pg_upgrade.

How this was checked

Commands, limits and prices were checked against these official pages, on November 23, 2026: