Postgres Database Backup

I am trying to get a working backup solution for Nextcloud just in case something happens. I have Nextcloud running behind a reverse proxy with docker-compose on an Ubuntu Server. Creating a backup for the data, or config folder is not the problem but I can’t figure out how to create a backup for my Postgres database. I have very little experience working with databases so I might be missing something trivial.

My docker-compose file looks like this

  version: '3.7'
  
  services:
    db_next:
      image: postgres:alpine
      restart: always
      volumes:
        - db_next:/var/lib/postgresql/data
      env_file:
        - ./env/nextcloud/.env.db

nextcloud:
  image: nextcloud:apache
  restart: always
  volumes:
    - nextcloud:/var/www/html/
    - nextcloud_data:/var/www/html/data
    - nextcloud_config:/var/www/html/config
    - nextcloud_apps:/var/www/html/custom_apps
  ports:
    - 8080:80
  env_file:
    - ./env/nextcloud/.env
  depends_on:
    - db_next
  networks:
    - proxy-tier
    - default

 volumes:
   db_next:
   nextcloud:
   nextcloud_data:
     driver: local
     driver_opts:
       type: none
       device: /mnt/cloud1/nextcloud/data
       o: bind
   nextcloud_config:
     driver: local
     driver_opts:
       type: none
       device: /mnt/cloud1/nextcloud/config
       o: bind
   nextcloud_apps:
     driver: local
     driver_opts:
       type: none
       device: /mnt/cloud1/nextcloud/apps
       o: bind

Google told me about SQL Dumps so i tried that one first. I already failed when trying to create the Backup file.

docker exec -t -u postgres your-db-container pg_dumpall -c > dump_date +%d-%m-%Y"_"%H_%M_%S.sql

With user postgres I get this error:

pg_dumpall: error: could not connect to database "template1": FATAL:  role "postgres" does not exist

I tried replacing the username with nextcloud since I have defined it as nextcloud in my .env file but then it complains that the user couldn’t be found.

I know that there is a section in the documentation about backup but I really have no idea what to do with that line:

PGPASSWORD="password" pg_dump [db_name] -h [server] -U [username] -f nextcloud-sqlbkp_`date +"%Y%m%d"`.bak

So my question is, how should I backup my database?

Use the same sql user/password nextcloud is using. If that fails it’s likely a docker issue (port, etc)

You mentioned a password. Where do I specify that? The command itself doesn’t ask for it.

it should be:

docker exec <nextcloud-db-container-name> pg_dump -c -U <nextcloud-db-user> <nextcloud-db> > /.../nextcloud-db.dump

you’ll find a script here:

hope you can read jinja templates. and you need restic install.