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 :

POSTGRES_DB

This 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_USER will be used.

Answer from lewer on Stack Overflow
🌐
Charles Desneuf
blog.charlesdesneuf.com › articles › creating-databases-when-starting-the-postgres-docker-ontainer
Creating databases when starting the Postgres Docker container | Charles Desneuf
November 21, 2023 - To create a ‘testing’ database automatically, you can use the following script: SELECT 'CREATE DATABASE testing' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'testing')\gexec · And here is an example of what to change in à docker-compose.yaml file: db: image: postgres...
Discussions

User and database not created with docker-compose
(Copy of my SO question) The docs explain that you can make the image create a user and database on creation, using environment variables. I can't seem to make that work using docker-compose: # doc... More on github.com
🌐 github.com
21
December 17, 2018
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 ... More on forums.docker.com
🌐 forums.docker.com
2
2
August 4, 2024
postgresql - Why database is not create when docker-compose up? - Stack Overflow
When i run docker-compose up -d --build creating is done, but i have one default database - postgres. And all 'create extension' is done in default database - postgres. Where is my mistake? ... The Postgres environment variables and initialization scripts are only used if Postgres doesn't find an existing ... More on stackoverflow.com
🌐 stackoverflow.com
Docker does not create the database with the correct name
Hello everyone. I am defining 2 containers, 2 services, with docker composer. One is postgres and the other is pgadmin. The problem is that when I enter the postgrest server credentials in pgadmin, the maintenance database, which I define in the .yml file as “portal”, does not accept the ... More on forums.docker.com
🌐 forums.docker.com
1
0
November 15, 2022
🌐
GitHub
gist.github.com › onjin › 2dd3cc52ef79069de1faa2dfd456c945
example docker compose for postgresql with db init script · GitHub
#!/bin/bash psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" -d "$POSTGRES_DB" <<-EOSQL create schema if not exists $SCHEMA; create table $SCHEMA.todos ( id serial primary key, done boolean not null default false, task text not null, due timestamptz ); create role $ANON nologin; create role $AUTHENTICATOR noinherit login password '$POSTGRES_PASSWORD'; grant $ANON to $AUTHENTICATOR; EOSQL · You can do this... create_tables.sql · CREATE DATABASE audit_service; GRANT ALL PRIVILEGES ON DATABASE audit_service to "postgres";
🌐
Medium
medium.com › @gantong.eu › the-docker-compose-file-for-deploying-databases-database-management-systems-and-creating-data-e8c08dd96e38
The Docker Compose File for Deploying Databases, Database Management Systems, and Creating Data Stores. | by Eakphisitdha Uttra | Medium
August 21, 2023 - 2. The path is /docker-compose/schema-postgres/data.sql · CREATE DATABASE IF NOT EXISTS database TEMPLATE template0 ENCODING 'UTF8'; \c database; CREATE TABLE IF NOT EXISTS beer ( id serial PRIMARY KEY, name varchar(255) COLLATE "en_US.utf8", ...
🌐
GitHub
github.com › docker-library › postgres › issues › 537
User and database not created with docker-compose · Issue #537 · docker-library/postgres
December 17, 2018 - docker-compose up -d --force-recreate postgresql docker-compose exec postgresql psql -U iotplatform # psql: FATAL: role "iotplatform" does not exist
Author   docker-library
🌐
Docker Community
forums.docker.com › general
PostgreSQL container created with docker compose - databse already exists - General - Docker Community Forums
August 4, 2024 - 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 s…
🌐
Docker
hub.docker.com › _ › postgres
postgres - Official Image | Docker Hub
The postgres database is a default database meant for use by users, utilities and third party applications. ... $ docker run -it --rm --network some-network postgres psql -h some-postgres -U postgres psql (14.3) Type "help" for help. postgres=# SELECT 1; ?column? ---------- 1 (1 row) Copy ... # Use postgres/example user/password credentials services: db: image: postgres restart: always # set shared memory limit when using docker compose shm_size: 128mb # or set shared memory limit when deploy via swarm stack #volumes: # - type: tmpfs # target: /dev/shm # tmpfs: # size: 134217728 # 128*2^20 bytes = 128Mb environment: POSTGRES_PASSWORD: example adminer: image: adminer restart: always ports: - 8080:8080 Copy
Find elsewhere
🌐
Kevin Kouomeu
1kevinson.com › how-to-create-a-postgres-database-in-docker
How to Create a Postgres Database in Docker
February 15, 2026 - PG_CONTAINER_NAME='postgres_tuto' POSTGRES_USER='tuto' POSTGRES_PASSWORD='admingres' POSTGRES_DB='tutos' PGDATA='/data/postgres-tuto' Now that our compose file is ready, we can create our SQL scripts file that must be copied in /docker-entrypoint-initdb.d/ -- CREATE TYPE DROP TYPE IF EXISTS genre; CREATE TYPE genre AS ENUM ( 'ADVENTURE', 'HORROR', 'COMEDY', 'ACTION', 'SPORTS' ); -- CREATE TABLE DROP TABLE IF EXISTS movies; CREATE TABLE movies ( id SERIAL PRIMARY KEY, title VARCHAR NOT NULL, release_year SMALLINT, genre genre, price NUMERIC(4, 2) );
🌐
Docker Community
forums.docker.com › docker engine › compose
Docker does not create the database with the correct name - Compose - Docker Community Forums
November 15, 2022 - Hello everyone. I am defining 2 containers, 2 services, with docker composer. One is postgres and the other is pgadmin. The problem is that when I enter the postgrest server credentials in pgadmin, the maintenance database, which I define in the .yml file as “portal”, does not accept the connection and it does accept it if the maintenance database name is “postgres” as you can see in the following images and if I set in Maintenance database “portal”. it show up that portal database does no...
🌐
Docker
docs.docker.com › guides › pre-seeding database with schema and data at startup for development environment
Pre-seeding database with schema and data at startup for development environment | Docker Docs
3 weeks ago - Make sure you stop any running Postgres containers (along with volumes) to prevent port conflicts before you follow the steps: ... CREATE TABLE IF NOT EXISTS users ( id SERIAL PRIMARY KEY, name VARCHAR(50), email VARCHAR(100) UNIQUE ); INSERT ...
🌐
Blog
blog.cadumagalhaes.dev › how-to-setup-a-postgresql-database-with-docker-compose
How to setup a PostgreSQL database with Docker Compose
February 5, 2025 - Once you are connected, you can right-click on Databases and create your own. In case you're not used to Postgresql, it has the concept of "schemas", and that's where you'll find your tables.
🌐
GitHub
github.com › docker-library › postgres › issues › 403
FATAL: database does not exist [docker compose] · Issue #403 · docker-library/postgres
January 26, 2018 - version: '2' services: postgres: image: "library/postgres:latest" environment: - POSTGRES_USER=test - POSTGRES_PASSWORD=qwerty - POSTGRES_DB=mydb ... docker run -it -e POSTGRES_USER=test -e POSTGRES_PASSWORD=qwerty -e POSTGRES_DATABSE=mydb postgres docker exec -it <CONTAINER_ID> /bin/bash psql -U test
Author   docker-library
🌐
Docker
docker.com › blog › how to use the postgres docker official image | docker
How to Use the Postgres Docker Official Image | Docker
November 6, 2024 - If no database exists when Postgres spins up in a container, it’ll create a default database for you. While this process unfolds, that database won’t accept any incoming connections. Working with a pre-existing database is best when using Docker Compose to start up multiple services.
🌐
OneUptime
oneuptime.com › home › blog › how to run postgresql in docker and docker compose
How to Run PostgreSQL in Docker and Docker Compose
January 21, 2026 - PostgreSQL Docker image automatically executes .sql, .sql.gz, and .sh files from /docker-entrypoint-initdb.d/ on first startup. ... -- Create extensions CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; CREATE EXTENSION IF NOT EXISTS "pg_stat_statements"; -- Create schemas CREATE SCHEMA IF NOT EXISTS app; -- Create tables CREATE TABLE IF NOT EXISTS app.users ( id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), email VARCHAR(255) UNIQUE NOT NULL, name VARCHAR(255) NOT NULL, created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() ); -- Create indexes CREATE INDEX IF NOT EXISTS idx_users_email ON app.users(email); CREATE INDEX IF NOT EXISTS idx_users_created_at ON app.users(created_at);
🌐
DEV Community
dev.to › saiful7778 › setting-up-postgresql-with-docker-compose-for-development-and-production-45j8
Setting Up PostgreSQL with Docker Compose for Development and Production - DEV Community
November 29, 2025 - -- ---------- 1. Create roles ---------- DO $$ BEGIN IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'db_migrator') THEN CREATE ROLE db_migrator LOGIN PASSWORD '12345678'; END IF; IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'db_rw') THEN CREATE ROLE db_rw LOGIN PASSWORD '12345678'; END IF; IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'db_ro') THEN CREATE ROLE db_ro LOGIN PASSWORD '12345678'; END IF; END $$; -- ---------- 2. Create databases ---------- CREATE DATABASE test; -- ============================================================ -- Apply permissions for each database
🌐
OneUptime
oneuptime.com › home › blog › how to set up postgresql with docker
How to Set Up PostgreSQL with Docker
February 2, 2026 - This is perfect for creating tables, ...s/01-create-tables.sql CREATE TABLE IF NOT EXISTS users ( id SERIAL PRIMARY KEY, email VARCHAR(255) UNIQUE NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE IF NOT EXISTS ...
🌐
Docker Community
forums.docker.com › general
Create docker container with postgres db and tables - General - Docker Community Forums
May 1, 2019 - I want to connect to this container from Python. I need to create a database and some tables while Docker is being created. I attached my docker files. I am running the command below: docker build -t postgres .