AlbertMunichMar
AlbertMunichMar

Reputation: 1906

docker-compose with postgres image not working

I am trying to have a nestjs app with a psql database dockerized. I have a docker-compose.yml:

# docker-compose.yml

version: '3.8'
services:
  
  postgres:
    image: postgres:13
    container_name: postgres_albert
    restart: always
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    volumes:
      - postgres:/var/lib/postgresql/data
    ports:
      - '5432:5432'

volumes:
  postgres:
    name: volume-uni

Then I access the container:

docker exec -it 36cf6358a019 bash

And try to use psql:

psql -U postgres

I get following error:

psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL:  role "postgres" does not exist

I don't understand, postgres is the official user and is there when installation. I used the official image without docker-compose with the postgres user and it worked. I tried also changing users but no success

Upvotes: 0

Views: 1284

Answers (1)

AlbertMunichMar
AlbertMunichMar

Reputation: 1906

I finally fix it by adding the POSTGRES_DB env var in the environment section of the docker-compose.yml. In the documentation is explained that if data folder is populated, it takes some values. Like the db name is the user etc.

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

Upvotes: 1

Related Questions