'Postgresql/Docker - password authentication failed for user "user"

I have set database with postgresql and docker, now when I want to access to my database I have this connection error saying password authentication failed for user "user". I already search answers but none helped me. Btw, I am new to this so I may have missed something. Here my docker-compose.yaml (which I'm not sure is configured correctly) :

version: "3"

services:
  database:
    image: postgres:13-alpine
    environment:
      POSTGRES_USER: main
      POSTGRES_PASSWORD: main
      POSTGRES_DB: main
    ports: [5432]

###> doctrine/doctrine-bundle ###
  database:
    image: postgres:13-alpine
    environment:
      POSTGRES_DB: db_app
      # You should definitely change the password in production
      POSTGRES_PASSWORD: secret_password
      POSTGRES_USER: symfony
    volumes:
      - db-data:/var/lib/postgresql/data:rw
      # You may use a bind-mounted host directory instead, so that it is harder to accidentally remove the volume and lose all your data!
      # - ./docker/db/data:/var/lib/postgresql/data:rw
###< doctrine/doctrine-bundle ###

volumes:
###> doctrine/doctrine-bundle ###
  db-data:
###< doctrine/doctrine-bundle ###

I also saw that it may be config in pg_hba file but don't know where it is located.

Any help ?



Solution 1:[1]

Why do you have two database services names? And which password do you use?

If you use POSTGRES_PASSWORD: main don't work because your password overwrite by the second database service.

docker-compose config

Output:

services:
  database:
    environment:
      POSTGRES_DB: db_app
      POSTGRES_PASSWORD: secret_password
      POSTGRES_USER: symfony
    image: postgres:13-alpine
    networks:
      default: null
    volumes:
    - type: volume
      source: db-data
      target: /var/lib/postgresql/data
      volume: {}
networks:
  default:
    name: tmp_default
volumes:
  db-data:
    name: tmp_db-data

If you connect with secret_password it's work.

Please remove one of the services and make sure you have a unique name for each service.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bassxzero