Have you tried setting POSTGRES_DB variable in your docker-compose.yml environment ?
image: postgres
environment:
POSTGRES_USER: ${POSTGRES_USER:-postgres}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-changeme}
POSTGRES_DB: premiership
PGDATA: /data/postgres
volumes:
see https://hub.docker.com/_/postgres :
Answer from lewer on Stack Overflow
POSTGRES_DBThis optional environment variable can be used to define a different name for the default database that is created when the image is first started. If it is not specified, then the value of
POSTGRES_USERwill be used.
User and database not created with docker-compose
PostgreSQL container created with docker compose - databse already exists
postgresql - Why database is not create when docker-compose up? - Stack Overflow
Docker does not create the database with the correct name
Have you considered using the POSTGRES_DB environment variable ?
services:
db:
container_name: postgres
image: postgres
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
PGDATA: /data/postgres
POSTGRES_DB: bank
volumes:
- db:/data/postgres
ports:
- "5332:5432"
networks:
- db
restart: unless-stopped
healthcheck:
test: [ "CMD-SHELL", "pg_isready -d postgres" ]
interval: 30s
timeout: 10s
retries: 5
networks:
db:
driver: bridge
volumes:
db:
You can use a create db script and add it as a volume, like this:
version: '3'
services:
db:
image: postgres:15.3-alpine3.18
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- '5432:5432'
volumes:
- ./db:/var/lib/postgresql/data
- ./create-db.sql:/docker-entrypoint-initdb.d/create_database.sql
networks:
- backnet
My SQL script is simple as this:
CREATE DATABASE some_database;
If you already have your volume initiated 'db', you'll need to erase it first (afaik) because postgres will declare that a database already exists and will skip initialization.
If, when you start your Docker Compose, you're getting:
PostgreSQL Database directory appears to contain a database; Skipping initialization
you need to proactively remove the volumes which were set up to store the database.
The command docker-compose down doesn't do this automatically.
You can request removal of volumes like this:
docker-compose down --volumes
Be warned that this will delete any data you had in any database before. You can't get this data back if you remove the volume which contained it!
According to the documentation of postgres docker image you did everything correct.
If you would like to do additional initialization in an image derived from this one, add one or more *.sql, *.sql.gz, or *.sh scripts under /docker-entrypoint-initdb.d (creating the directory if necessary). After the entrypoint calls initdb to create the default postgres user and database, it will run any *.sql files, run any executable *.sh scripts, and source any non-executable *.sh scripts found in that directory to do further initialization before starting the service.
But, there is a catch which I think you missed based on log that you posted above.
Warning: scripts in /docker-entrypoint-initdb.d are only run if you start the container with a data directory that is empty; any pre-existing database will be left untouched on container startup.
So, I would give it a try to empty database_data directory and run again docker-compose up.