Upgrade nextcloud postgres container

You're using nextcloud with docker, have your dependencies pinned, and run a bot to upgrade them, and there comes the major postgres upgrade, you hit the merge button and nextcloud blows !

So there is this excellent post that describes quite well the process, but while doing it it failed, and indeed it's missing a little point, at least this was the case for me. This post aims at clarifying it.

So let's start.

You first have your docker-compose file, that has the nextcloud container, tied to the db container:

volumes:
  nextcloud:
  db:

services:
  db:
    image: postgres:12
    restart: always
    volumes:
      - db:/var/lib/postgresql/data
    env_file:
      - db.env
  app:
    image: nextcloud:23.0.2
    restart: always
    depends_on:
      - db
      - redis
    volumes:
      - nextcloud:/var/www/html

The 1st thing you do is to add a db2 container with the upgraded version of postgres

  db2:
    image: postgres:14
    restart: always
    volumes:
      - db2:/var/lib/postgresql/data
    env_file:
      - db.env

ok now, this is where the post is a little bit confusing, this new container will be created with the db.env ie:

POSTGRES_DB=nextcloud
POSTGRES_HOST=db
POSTGRES_PASSWORD=A_PASSWORD
POSTGRES_USER=postgres

if you run the command from the post right now, to dump the database from the old container and pipe it to the new container it will fail, because nextcloud is not using the user you defined with the POSTGRES_USER and POSTGRES_PASSWORD

So you need to look at your config.php inside the nextcloud container which credentials nextcloud is using:

❯ dce -T app cat config/config.php | grep db
  'dbtype' => 'pgsql',
  'dbname' => 'nextcloud',
  'dbhost' => 'db',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'dbuser' => 'oc_admin',
  'dbpassword' => 'PASSWORD',

so now that you have those, you need to create inside the db2 container this oc_admin user

❯ dce -T db2 psql -U postgres

then inside psql in the db2 container:

psql (14.1 (Debian 14.1-1.pgdg110+1))
Type "help" for help.

postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

postgres=# create user oc_admin with password 'PASSWORD' createdb;
CREATE ROLE
postgres=# 

And now only you're set to transfer the dump from db to db2 !

❯ dce -T db pg_dump -U oc_admin -d nextcloud | dce -T db2 psql -U oc_admin -d nextcloud

After that stop the containers dcstop then edit the config.php in the volume:

❯ sudo vi /var/lib/docker/volumes/nextcloud_nextcloud/_data/config/config.php

and change the 'dbhost' => 'db', to 'dbhost' => 'db2',

You're set, nextcloud went from version 12 to 14 without any issue