Nextcloud - Upgrading PostgreSQL database running as Docker container between major versions

Published on Author admin

How to upgrade PostgreSQL database running as Docker container between major versions.
Procedure useful for migrating Nextcloud PostgreSQL 13 container database to PostgreSQL 15 database.

Naming convention:
nextcloud-postgres13 - container with PostgreSQL 13 database
nextcloud-postgres15 - container with PostgreSQL 15 database

Before performing migration start new container with PostgreSQL 15 database.
Following docker-compose YAML file can be used:

  nextcloud-postgres:
    container_name: nextcloud-postgres15
    hostname: nextcloud-postgres15
    image: postgres:15-alpine
    restart: always
    volumes:
      - /data/nextcloud-postgresql15-data:/var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD=your_database_password
      - POSTGRES_DB=nextcloud
      - POSTGRES_USER=nextcloud

Dump data from PostgreSQL 13 database:

docker exec -it nextcloud-postgres13 pg_dumpall -U nextcloud > nextcloud-postgres-2022-12-20.sql

Inport data to new PostgreSQL 15 database:

docker exec -i nextcloud-postgres15 psql -U nextcloud < nextcloud-postgres-2022-12-20.sql

After migration, check Nextcloud logs

docker logs nextcloud-postgres

Errors which can be found after upgrading PostgreSQL to version 15:

2022-12-20 21:05:55.342 UTC [1] LOG:  database system is ready to accept connections
2022-12-20 21:06:19.959 UTC [29] FATAL:  password authentication failed for user "nextcloud"
2022-12-20 21:06:19.959 UTC [29] DETAIL:  User "nextcloud" does not have a valid SCRAM secret.
    Connection matched pg_hba.conf line 100: "host all all all scram-sha-256"

Fix SCRAM secret after upgrading PostgreSQL to version 15:

docker exec -it nextcloud-postgres15 /bin/bash
psql -U nextcloud
SELECT
    rolname, rolpassword ~ '^SCRAM-SHA-256\$' AS has_upgraded
FROM pg_authid
WHERE rolcanlogin;

change PostgreSQL password

\password