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
java - postgres database doesn't exist in docker container - Stack Overflow
When I built my app image everything was ok, then I should start my db image and app image. I start db with docker-compose up db and I got: org.postgresql.util.PSQLException: FATAL: database "haircut_studio" does not exist. Is something wrong with my DbConfig? More on stackoverflow.com
🌐 stackoverflow.com
PostgreSQL docker-compose: FATAL: database does not exist - Stack Overflow
I'm getting the following error with postgreSQL with docker-compose: postgres_1 | 2021-11-26 18:02:47.131 UTC [138] FATAL: database "preferences" does not exist Just run docker-compos... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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...
🌐
Stack Overflow
stackoverflow.com › questions › 70128494 › postgresql-docker-compose-fatal-database-does-not-exist
PostgreSQL docker-compose: FATAL: database does not exist - Stack Overflow
@banan3'14 I had to create a script which creates the DB manually. You can also use this image instead postgres hub.docker.com/_/postgres ... Does this answer your question? Docker-Compose + Postgres: /docker-entrypoint-initdb.d/init.sql: Permission denied
🌐
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 ... 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
🌐
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 …
🌐
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
🌐
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:
🌐
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.
🌐
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....
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/

🌐
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
🌐
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
🌐
Docker Community
forums.docker.com › docker desktop
Postgres container not starting for docker desktop windows - Docker Desktop - Docker Community Forums
December 10, 2022 - Hi, My name is Pristal Jane. I have just started using Docker. I was setting up postgres using docker compose. Here is the file. version: "3.8" services: postgres: image: "postgres:14.1-alpine" ports: - 5432:5432 environment: POSTGRES_USER: "abc" POSTGRES_PASSWORD: "abc" POSTGRES_DB: "default" volumes: - d:/postgressql/data:/var/lib/postgresql/data When I run this using command: docker-compose up -d container is up and running.
Top answer
1 of 5
156

The problem was simple enough that my computer was already running an instance of Postgres that I was not aware was still running (not inside Docker) on :5432, checked with:

$ lsof -n -i:5432 | grep LISTEN

So I remembered I installed it via https://gist.github.com/sgnl/609557ebacd3378f3b72, I ran

$ pg-stop

And then I had no problem connecting to the Docker instance.

Edit (2019/07/02)

This question recently passed 10,000 views, so I thought I should elaborate more on why this happened.

Usually running through docker, using python and connecting to a postgres database requires you to install psycopg2, via pip3 install psycopg2, but if you run this command you will get:

Error: pg_config executable not found.

This is because psycopg2 requires an operating system install of the postgres libraries:

yum install postgresql-devel
apt-get install postgresql-client

Now, on a Mac, you will need to do the same with brew:

brew install postgresql

One thing that I didn't realise, is that on Mac, doing the above will not only install required libraries, but also start a database on :5432. Because this was all done in the background, it didn't occur to me that this was the problem as none of the usual errors popped up to inform that the port was being used, etc...

2 of 5
36

In my case, the problem may be due also to the use of a super user who is not postgres. Lhe developers chose the user georchestra in the geOrchestra docker composition. Extract from the dockerfile:

environment:
  - POSTGRES_USER=georchestra

HEALTHCHECK --interval=30s --timeout=30s \
  CMD pg_isready -U $POSTGRES_USER

consequently, you will have to use the option "-U georchestra" to connect to the container.

$ docker exec -it docker_database_1 psql -U georchestra
psql (10.5 (Debian 10.5-1.pgdg90+1))
Type "help" for help.

and of course, trying to connect with the user postgres causes an error:

docker exec -it docker_database_1 psql -U postgres
psql: FATAL:  role "postgres" does not exist
🌐
Docker Community
forums.docker.com › docker engine › compose
Docker database disappears randomly - Compose - Docker Community Forums
August 28, 2025 - Migrated an application to docker consisting mainly of a webapp container and a postgres container, the latter using the official image postgres:17.6. Snippet from compose.yaml: dbservice: container_name: pg image: postgres:17.6 restart: always ...