PostgreSQL container created with docker compose - databse already exists

Hi,
One of my containers defined in docker-compose.yml file is PostgreSQL database. I am trying to initialize it with .sql script mounted on /docker-entrypoint-initdb.d/. Docker-compose works fine, but PostgreSQL logs say that my custom name database (guacamole_db) already exists despite it being freshly started container and skips further initialization. Why would that be?
My docker-compose file:

version: '3'

services:
  guacd:
    container_name: guacamole-guacd
    restart: always
    image: guacamole/guacd
    networks:
      - guacamole

  guacamole:
    container_name: guacamole-guacamole
    restart: always
    image: guacamole/guacamole
    ports:
      - "8080:8080"
    environment:
      - GUACD_HOSTNAME=guacd
      - POSTGRESQL_HOSTNAME=database
      - POSTGRESQL_DATABASE=${GUACAMOLE_DATABASE}
      - POSTGRESQL_USER=${GUACAMOLE_USER}
      - POSTGRESQL_PASSWORD=${GUACAMOLE_PASSWORD}
    links:
      - guacd
      - database
    networks:
      - guacamole

  database:
    container_name: guacamole-db
    restart: always
    image: postgres
    environment:
      - POSTGRES_USER=${GUACAMOLE_USER}
      - POSTGRES_DB=${GUACAMOLE_DATABASE}
      - POSTGRES_PASSWORD=${GUACAMOLE_PASSWORD}
    volumes:
      - ./db:/var/lib/postgresql/data:rw
      - ./initdb:/docker-entrypoint-initdb.d/:ro
    networks:
      - guacamole

networks:
  guacamole:
    driver: bridge
    name: guacamole-network
    ipam:
      driver: default
      config:
        - subnet: "172.16.30.0/24"

PostgreSQL logs:

server started
CREATE DATABASE


/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/01-create-db.sql
psql:/docker-entrypoint-initdb.d/01-create-db.sql:1: ERROR:  database "guacamole_db" already exists
2024-08-04 14:37:25.151 UTC [62] ERROR:  database "guacamole_db" already exists
2024-08-04 14:37:25.151 UTC [62] STATEMENT:  CREATE DATABASE guacamole_db;

PostgreSQL Database directory appears to contain a database; Skipping initialization

Are you aware that the container will use the name from the environment variable POSTGRES_DB to create the database on first start of the container. But it will only do so, if the volume (in your case a bind of the local folder ./db) is empty.

What exactly does your sql script in initdb.d do? If it just creates the database and a user, then it tries to do, what is already done.

Since we have no idea what you initdb sql script does, we can only guess what might be the problem.

Hello

Something to test in your case is to remove - ./db:/var/lib/postgresql/data:rw from your yaml file to no more keep a local copy of the DB on your host.