Postgres Setup Fail

The Basics

  • Nextcloud Server version (e.g., 29.x.x):
    • fpm-30
  • Operating system and version (e.g., Ubuntu 24.04):
    • debian bookworm
  • Web server and version (e.g, Apache 2.4.25):
    • nginx:latest
  • Reverse proxy and version _(e.g. nginx 1.27.2)
    • caddy:latest
  • PHP version (e.g, 8.3):
    • the one from the image
  • Is this the first time you’ve seen this error? (Yes / No):
    • yes
  • When did this problem seem to first start?
    • when I tried creating my nextcloud stack
  • Installation method (e.g. AlO, NCP, Bare Metal/Archive, etc.)
    • Docker
  • Are you using CloudfIare, mod_security, or similar? (Yes / No)
    • no

Summary of the issue you are facing:

So I am trying to create a Docker Stack with docker compose for my nextcloud application. I used maria DB for some years now in my old instalation but I am trying to refresh that shit.
Thats why I opted to do a new install with postgres:16, nginx, redis:7 and the nextcloud:fpm-30 image.

But doing that seems to be rather complicated. I havent used a custom postgres install before but I figured, that I need to modify the pg_hba.conf to allow my nextcloud container to talk with the DB.
Also figured out, that I want to update the postgresql.conf Mostly for SSL and authentication settings.

Since I am a puppet user, I created a small puppet class for the general nextcloud setup as well as for the postgres part. But after trying to roll it out for the first time, I ran into some issues.

  1. The postgres container skips initialization since there are already files present in /var/lib/postgresql/data these files are just my config files that are placed before the container is created via a bind mount.
  2. The nextcloud container fails to connect to the db container since it cant resolve the POSTGRES_HOST. This seems to be due to the fact that the postgres container for some arbitrary reason is not joined to the nextcloud_internal network. This in turn stops the nextcloud container from doing the fist install steps and nothing works.

Log entries

nextcloud-app-1    | Error while trying to create admin account: An exception occurred in the driver: SQLSTATE[08006] [7] connection to server at "db" (172.20.0.2), port 5432 failed: Connection refused
nextcloud-db-1 | PostgreSQL Database directory appears to contain a database; Skipping initialization

Here is the docker-compose.yml, pg_hba.conf, postgresql.conf, and my puppet code:
nextcloud-setup ($4798696) · Snippets · GitLab

PS: If I dont apply any custom stuff to the postgres, It is unable to connect to the nextcloud container. Then the container refuses the conenction from the nextcloud container.

I havent used a custom postgres install before but I figured, that I need to modify the pg_hba.conf to allow my nextcloud container to talk with the DB.

Nope. The postgres image’s entrypoint already does this (and scram-sha-256 is the default these days).

Also figured out, that I want to update the postgresql.conf Mostly for SSL and authentication settings.

I would suggest doing this by either:

  • (a) taking your postgresql.conf config file and injected it elsewhere (e.g. /etc/postgresql/postgresql.conf. Then specifying -c config_file=/etc/postgresql/postgresql.conf as the command in your Compose.
  • (b) outright specifying all the custom values via -c

See Docker - Postgresq: Database Configuration for the suggested approach for the image.

The nextcloud container fails to connect to the db container since it cant resolve the POSTGRES_HOST. This seems to be due to the fact that the postgres container for some arbitrary reason is not joined to the nextcloud_internal network. This in turn stops the nextcloud container from doing the fist install steps and nothing works.

I don’t think that’s what is going on here. Your networks config looks reasonable based on your linked snippets. And the message is “Connection refused”, which is more what might happen if the postgres container is still initializing and/or there is some interaction with your customized config. You’d have to cross-reference the “Connection refused” with the state of the postgresql container at the same time (by checking the db container log) to know for certain.

When testing, in between test runs, make sure you clear out the contents of your volumes (for both db and app containers). Otherwise tests will be invalid since there will be content from prior test runs in them.

Personally I’d go back to basics: dump the SSL stuff and all the customizations. Make sure a simple stack comes up and functions. If that works, nuke the volumes, and start layering in your changes. For a working example, the Compose-based stack here is a good starting point.

P.S. Unless I’m overlooking something, you aren’t providing the Redis password to Nextcloud. See the image’s Redis configuration section.

4 Likes