nextcloud postgres pg_dump pg_dumpall psql

Source

  • stop nextcloud
  • backup the whole database (including users, etc.)
docker compose exec -T db pg_dumpall -U $(cat buildfiles/postgres_user.txt) > pg_dumpall.sql
  • change the docker-compose.yml file duplicating the current ‘db’ service, renaming it to e.g. ‘old_db’, incrementing the version number of the image for the new db service (still named ‘db’, so that nextcloud doesn’t need to be touched), and creating and using a new volume for the new db service (so that in case smth. goes wrong we can easily switch back to the last working db container)
  • recreate the db containers
  • restore the backup in the new container (named ‘db’; the old one is named ‘old_db’!)
cat pg_dumpall.sql | docker compose exec -T db psql -U $(cat buildfiles/postgres_user.txt) template1
  • start nextcloud and hope for the best

Note

  • the -T for docker compose is necessary because psql does not need a TTY when used like this; it outputs the SQL commands directly to STDOUT