sudo systemctl stop apache2.service sudo -i -u postgres pg_dump nextcloud_db > ./nextcloud_db_20230809_2.sql sudo tar -cvzf nextcloud-dir-backup-20230809.tar.gz /var/www/nextcloud

sudo zfs snapshot site1-zpool1/Data-Nextcloud@20230809-migration

Create your docker compose file

https://github.com/docker/awesome-compose/blob/master/nextcloud-postgres/compose.yaml

Find the right docker release tag for your setup: https://hub.docker.com/_/nextcloud/tags?page=1

docker-compose exec db sh -c “PGPASSWORD=‘PASSWORD’ psql -U ncadmin -d nextcloud_db -f /dmp” docker-compose exec db rm /dmp

shut down the container

$ mv nextcloud nextcloud_original
$ mkdir nextcloud

Extract nextcloud-dir-backup-20230809.tar.gz into the ./nextcloud folder you just created

$ sudo chown www-data:www-data nextcloud
$ sudo chmod 755 nextcloud

$ docker compose exec db psql -U ncadmin -d nextcloud_db -c “\du”
List of roles Role name | Attributes | Member of ———–+————————————————————+———– ncadmin | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

Errors

nextcloud-db-1 | 2023-08-09 03:55:58.483 UTC [44] FATAL: database “ncadmin” does not exist nextcloud-db-1 | 2023-08-09 03:56:54.143 UTC [54] FATAL: database “ncadmin” does not exist nextcloud-db-1 | 2023-08-09 03:56:58.832 UTC [57] FATAL: role “root” does not exist nextcloud-db-1 | 2023-08-09 03:57:20.883 UTC [60] FATAL: database “ncadmin” does not exist nextcloud-db-1 | 2023-08-09 03:58:50.245 UTC [66] FATAL: role “postgres” does not exist nextcloud-db-1 | 2023-08-09 03:59:56.385 UTC [27] LOG: checkpoint starting: time

The error message indicates that it’s trying to connect to a database named “ncadmin”, which does not exist. By default, psql tries to connect to a database with the same name as the user, in this case “ncadmin”.

Quick workaround: we are going to rename both the db and user as “nextcloud”.

Step 1: we copy the db to a new one

$ docker compose exec db psql -U ncadmin -d nextcloud_db -c “CREATE DATABASE nextcloud WITH TEMPLATE nextcloud_db;”

Step 2: we create our new user

docker compose exec db psql -U ncadmin -d nextcloud -c “CREATE USER nextcloud WITH PASSWORD ‘PASSWORD’;” docker compose exec db psql -U ncadmin -d nextcloud -c “GRANT ALL PRIVILEGES ON DATABASE nextcloud TO nextcloud;” docker compose exec db psql -U ncadmin -d nextcloud -c “ALTER USER nextcloud WITH SUPERUSER CREATEDB CREATEROLE REPLICATION BYPASSRLS;”

This is what we should now see:

$ docker compose exec db psql -U ncadmin -d nextcloud -c “\du”
List of roles Role name | Attributes | Member of ———–+————————————————————+———– ncadmin | Superuser, Create role, Create DB, Replication, Bypass RLS | {} nextcloud | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

Edit your config.php accordingly, with the new db username, password, and db name. Change the db host as well to ‘db’.

Comment out components that aren´t installed (like redis) and “array” blocks that might create issues.

Start your new container.

Add a cron to the host system’s crontab

*/5 * * * * docker exec -u www-data nextcloud-app-1 php /var/www/html/cron.php

Upgrade

Edit the tag from your compose file. Do not bump more than one version at a time (i.e 25.x to 26.x).

https://github.com/nextcloud/docker#update-to-a-newer-version

Fixes and optimizations: check indications under the admin pannel

Missing db indices

$ docker exec -u www-data nextcloud-app-1 php occ db:add-missing-indices

Work through the issues one by one. A lot will show up after such migration.