You most probably started the container without the env variables set. The db was then initialized without user or DB info.

You need to delete the volume and try again.

Credit: https://github.com/docker-library/postgres/issues/453#issuecomment-393939412

Answer from howaryoo on Stack Overflow
Discussions

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
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
postgresql - create postgres database if it does not exist on docker-compose start up - Stack Overflow
PGDATA is the location inside the container for the database files. If you have no particular reason to change it, you should just leave the default /var/lib/postgresql/data and replace your volume mapping with postgres:/var/lib/postgresql/data Have you tried to remove both the container and the volume, and restart the service ? You can do it with docker-compose ... More on stackoverflow.com
🌐 stackoverflow.com
User and database not created with docker-compose
The docs explain that you can make ... user and database on creation, using environment variables. I can't seem to make that work using docker-compose: # docker-compose.yml services: postgresql: image: postgres:alpine environment: POSTGRES_DB: iotplatform POSTGRES_USER: iotplatform POSTGRES_PASSWORD: iotplatform ... docker-compose up -d --force-recreate postgresql docker-compose exec postgresql psql -U iotplatform # psql: FATAL: role "iotplatform" does not exist... More on github.com
🌐 github.com
21
December 17, 2018
🌐
Reddit
reddit.com › r/docker › postgresql in docker: database "admin" does not exist error
r/docker on Reddit: PostgreSQL in docker: database "admin" does not exist error
January 14, 2024 -

Hi,
I'm running the alpine PostgreSQL image in docker using the following docker-compose file below. After building and starting, the container gives the following fatal error message: "FATAL: database "admin" does not exist". After some googling it looks like postgresql needs a database to exist same to the user name that I declared in the docker-compose. Anyone an idea how to solve the error?

version: '3.8'

x-systeminfo: &systeminfo
  # Database
  POSTGRES_ADDRESS: database
  POSTGRES_DB: dev
  POSTGRES_USER: admin
  POSTGRES_PASSWORD: ****************
  POSTGRES_PORT: 5432

services:
  database:
build: ./Database
restart: always
ports:
   - 5432:5432
environment: *systeminfo
volumes:
   - postgresqldata:/var/lib/postgresql/data
healthcheck:
  test: pg_isready -U $$POSTGRES_USER
  interval: 10s
  timeout: 5s
  retries: 20

volumes:
  postgresqldata:

The docker file inside ./Database looks like this:

FROM postgres:alpine
COPY . /docker-entrypoint-initdb.d/
🌐
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 say that my custom name database (guacamole_db) already exists despite it being freshly started container and skips further initialization.
🌐
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...
🌐
GitHub
github.com › docker-library › postgres › issues › 537
User and database not created with docker-compose · Issue #537 · docker-library/postgres
December 17, 2018 - 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: # docker-compose.yml services: postgresql: image: postgres:alpine environment: POSTGRES_DB: iotplatform POSTGRES_USER: iotplatform POSTGRES_PASSWORD: iotplatform ... docker-compose up -d --force-recreate postgresql docker-compose exec postgresql psql -U iotplatform # psql: FATAL: role "iotplatform" does not exist
Author   docker-library
Find elsewhere
🌐
GitHub
github.com › langgenius › dify › issues › 8147
database "postgres" does not exist · Issue #8147 · langgenius/dify
September 9, 2024 - docker-db-1 | 2024-09-09 08:39:54.594 UTC [1] LOG: starting PostgreSQL 15.8 on aarch64-unknown-linux-musl, compiled by gcc (Alpine 13.2.1_git20240309) 13.2.1 20240309, 64-bit docker-db-1 | 2024-09-09 08:39:54.595 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432 docker-db-1 | 2024-09-09 08:39:54.595 UTC [1] LOG: listening on IPv6 address "::", port 5432 docker-db-1 | 2024-09-09 08:39:54.606 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" docker-db-1 | 2024-09-09 08:39:54.721 UTC [37] LOG: database system was shut down at 2024-09-09 08:29:08 UTC docker-db-1 | 2024-09-09 08:39:54.794 UTC [1] LOG: database system is ready to accept connections docker-web-1 | pm2 launched in no-daemon mode (you can add DEBUG="*" env variable to get more messages)
Author   langgenius
🌐
Medium
medium.com › @chunkang.wong94 › how-to-fix-the-role-postgres-does-not-exist-error-when-deploying-postgresql-docker-container-430e903b97fd
How to Fix the “Role ‘Postgres’ Does Not Exist” Error When Deploying PostgreSQL Docker Container | by ChunKang Wong | Medium
February 27, 2024 - How to Fix the “Role ‘Postgres’ Does Not Exist” Error When Deploying PostgreSQL Docker Container TL; DR: First check port usage on port 5432 using command sudo lsof -i :5432, there should be …
🌐
DEV Community
dev.to › frio_16 › fixing-fatal-database-does-not-exist-in-postgresql-docker-healthcheck-mistake-3il8
🐳 Fixing “FATAL: database does not exist” in PostgreSQL Docker (Healthcheck Mistake) - DEV Community
February 14, 2026 - services: postgres: image: postgres:16 container_name: train-postgres environment: POSTGRES_DB: train_db POSTGRES_USER: train_user POSTGRES_PASSWORD: train_pass ports: - "5433:5432" volumes: - train_pgdata:/var/lib/postgresql/data healthcheck: test: ["CMD-SHELL", "pg_isready -U train_user"] interval: 5s timeout: 5s retries: 5 api: build: . container_name: train-api ports: - "8080:8080" depends_on: postgres: condition: service_healthy environment: ConnectionStrings__DefaultConnection: "Host=postgres;Port=5432;Database=train_db;Username=train_user;Password=train_pass" volumes: train_pgdata:
🌐
GitHub
github.com › docker-library › postgres › issues › 756
with docker-compose.yml "Root or postgres or any role does not exist", postgres, postgrest, swagger connect to db · Issue #756 · docker-library/postgres
August 26, 2020 - version: '3.5' services: server: image: postgrest/postgrest ports: - "3000:3000" links: - db:db environment: PGRST_DB_URI: postgres://app_user:password@db:5432/app_db PGRST_DB_SCHEMA: public PGRST_DB_ANON_ROLE: app_user #In production this role should not be the same as the one used for the connection PGRST_SERVER_PROXY_URI: "http://127.0.0.1:3000" depends_on: - db container_name: server db: image: postgres ports: - "5432:5432" environment: POSTGRES_DB: app_db POSTGRES_USER: app_user POSTGRES_PASSWORD: password # Uncomment this if you want to persist the data.
Author   docker-library
🌐
Stack Overflow
stackoverflow.com › questions › 71059557 › docker-fatal-database-does-not-exist
postgresql - Docker - "FATAL: database does not exist" - Stack Overflow
February 10, 2022 - You're not using an image built from the repository to which you just linked. The official postgres image doesn't support POSTGRES_MULTIPLE_DATABASES so it's not creating any of your databases.
🌐
GitHub
github.com › docker-library › postgres › issues › 1055
Role "postgres" does not exist · Issue #1055 · docker-library/postgres
March 6, 2023 - I am trying to connect a Django server to a PSQL database but it seems like the database is not created with the correct user in my docker-compose.yml file. I get the following error: gestion_materiel_tps-db-1 | 2023-03-06 08:19:53.351 UTC [70] FATAL: password authentication failed for user "postgres" gestion_materiel_tps-db-1 | 2023-03-06 08:19:53.351 UTC [70] DETAIL: Role "postgres" does not exist.
Author   docker-library
🌐
Connect
connect.hyland.com › t5 › alfresco-forum › fatal-database-quot-alfresco-quot-does-not-exist › td-p › 139738
FATAL: database "alfresco" does not exist - Hyland Connect - 139738
March 22, 2022 - acs-postgres: image: postgres:9.6 ... the projects. I am available to give more information if necessary to solve my problem. ... You database creation is not happening properly....
🌐
Stack Overflow
stackoverflow.com › questions › 64423866 › docker-compose-up-fatal-database-boilerplate-does-not-exist-postgres-node
node.js - docker-compose up: FATAL: database "boilerplate," does not exist (Postgres Node) - Stack Overflow
When the node app tries to access ... actually exists, it gives the error: ... However, when I connect to the postgres container (using docker exec -it <postgres_container_ID> psql -U postgres) and list databases with \l, it shows the database boilerplate listed. What am I missing? How come the database shows up but the node server cannot find it? Thank you in advance. ... Sign up to request clarification or add additional context in comments. ... Wow, I don't know how I did not see that and ...
🌐
Medium
medium.com › codemonday › docker-your-first-postgresql-connection-in-10-mins-90627a199f87
Docker: Your First PostgreSQL Connection in 10 mins | by tanut aran | CODEMONDAY | Medium
September 8, 2022 - $ docker compose -f ./path/to/docker-compose.yml up · Download the PostgreSQL client name psql and use it to test the connection · $ psql -h localhost -p 5436 -U admin -d my_database
Top answer
1 of 1
1

Are you sure your database init script is getting called? The postgresql docker uses files in /docker-entrypoint-initdb.d/* as you can see in the Dockerfile here: https://github.com/docker-library/postgres/blob/443c7947d548b1c607e06f7a75ca475de7ff3284/9.4/docker-entrypoint.sh#L77

You can test this by bringing up ONLY the database, by running something like

docker-compose up db

which will ONLY bring up the db container, and then you can docker exec -it in and psql into the postgres processes, or use your exposed port on 5432

Also, you may want to pass in the initialization of the database, username, and password the way the Dockerfile expects them, as env variables as you can see in the same file

Environment Variables The PostgreSQL image uses several environment variables which are easy to miss. While none of the variables are required, they may significantly aid you in using the image.

POSTGRES_PASSWORD This environment variable is recommended for you to use the PostgreSQL image. This environment variable sets the superuser password for PostgreSQL. The default superuser is defined by the POSTGRES_USER environment variable. In the above example, it is being set to "mysecretpassword".

POSTGRES_USER This optional environment variable is used in conjunction with POSTGRES_PASSWORD to set a user and its password. This variable will create the specified user with superuser power and a database with the same name. If it is not specified, then the default user of postgres will be used.

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.

https://hub.docker.com/_/postgres/