docker-library / postgres

Docker Official Image packaging for Postgres
http://www.postgresql.org
MIT License
2.16k stars 1.13k forks source link

Error in log - FATAL: database "armkdbd" does not exist #1092

Closed AlexSSP closed 1 year ago

AlexSSP commented 1 year ago

Hello! When I put in my environment of docker-compose file image: postgres:15.3-alpine environment: POSTGRES_DB: arm-bd-db POSTGRES_USER: armkdbd POSTGRES_PASSWORD: armkdbd_password container started, database arm-bd-db created, but i see in my docker container log FATAL: database "armkdbd" does not exist. Why is this happening?

th0th commented 5 months ago

@AlexSSP why did you close this? Did you find the issue?

yosifkit commented 5 months ago

I'd guess that this stems from POSTGRES_DB being different than the POSTGRES_USER. If you use the postgres cli (and I'd assume many libraries), then if you don't specify which database to connect to, it will default to using a database named after the specified user.

$ docker run -it --rm -e 'POSTGRES_PASSWORD=5678' -e 'POSTGRES_USER=not-postgres' -e 'POSTGRES_DB=foo' --name pg postgres:16
...
2024-04-08 21:12:28.704 UTC [1] LOG:  database system is ready to accept connections
2024-04-08 21:13:26.690 UTC [75] FATAL:  database "not-postgres" does not exist
$ # in another terminal
$ docker exec -it pg psql --username not-postgres
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL:  database "not-postgres" does not exist
$ docker exec -it pg psql --username not-postgres --dbname foo
psql (16.2 (Debian 16.2-1.pgdg120+2))
Type "help" for help.

foo=#
\q