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 OverflowYou 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
In my case, I've already had PostgreSQL working in the background on windows. And when I tried to connect to the docker container, I connected to my windows Postgres. You need to open services.msc and turn off PostreSQL
PostgreSQL container created with docker compose - databse already exists
Docker does not create the database with the correct name
postgresql - create postgres database if it does not exist on docker-compose start up - Stack Overflow
User and database not created with docker-compose
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/
If you don’t specify the PGUSER environment variable, then psql will assume you want to use the current OS user as your database user name. In this case, you are using root as your OS user, and you will attempt to log in as root, but that user doesn’t exist in the database.
You’ll need to either call psql -U postgres, or su - Postgres first
See also the postgresql documentation
UPDATE: Someone suggested changing PGUSER to POSTGRES_USER -- this is actually incorrect. Postgres looks for PGUSER in the environment, but if you're using Docker, you'll tell Docker the correct user by using POSTGRES_USER, which gets assigned to PGUSER -- see the entrypoint source code
I specified user: postgres for the service in docker-compose file. Then deleted the existing container to re-spin it with the next docker-compose up execution. Container came up with the user "postgres", so you just need psql -l from there onwards(don't need -U flag)
This was my docker-compose file
version: '3.1'
services:
db:
image: postgres:12.6-alpine
restart: always
container_name: postgres12_6
user: postgres
environment:
- "POSTGRES_PASSWORD=postgres"
- "ES_JAVA_OPTS=-Xms1024m -Xmx3072m"
networks:
- esnet
adminer:
image: adminer
restart: always
ports:
- 8080:8080
networks:
esnet: