Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker password authentication failed for user "postgres"

I'm writing a docker-compose file to launch some services. But the db service is a trouble maker, I always get this error:

FATAL: password authentication failed for user "postgres" DETAIL: Password does not match for user "postgres". Connection matched pg_hba.conf line 95: "host all all all md5"

I've read a lot of threads, and I've correctly set the POSTGRES_USER and POSTGRES_PASSWORD. I have also remove the previous volumes and container to force postgresql to re-init the password. But I can't figure out why it's still not working.

So what is the correct way to force the re-initialization of the postgresql image. So I would be able to connect to my database.

I've seen that this error: Connection matched pg_hba.conf line 95: "host all all all md5", and I've heard about the postgres conf file. But it's an official container it's supposed to work, isn't it ?

version: '3'
services:
  poll:
    build: poll
    container_name: "poll"
    ports:
      - "5000:80"
    networks:
      - poll-tier
    environment:
      - REDIS_HOST=redis
    depends_on:
      - redis

  worker:
    build: worker
    container_name: "worker"
    networks:
      - back-tier
    environment:
      - REDIS_HOST=redis
      - POSTGRES_HOST=db
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=root
    depends_on:
      - redis
      - db

  redis:
    image: "redis:alpine"
    container_name: "redis"
    networks:
      - poll-tier
      - back-tier

  result:
    build: result
    container_name: "result"
    ports:
      - "5001:80"
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=root
      - POSTGRES_HOST=db
      - RESULT_PORT=80
    networks:
      - result-tier
    depends_on:
      - db

  db:
    image: "postgres:alpine"
    container_name: "db"
    restart: always
    networks:
      - back-tier
      - result-tier
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=root
      - POSTGRES_DB=postgres

volumes:
  db-data:
    driver: local

networks:
  poll-tier: {}
  back-tier: {}
  result-tier: {}

I'm expected to get the db connected, and not password authentication failed for user "postgres".

like image 943
Lucas Gras Avatar asked Oct 25 '19 16:10

Lucas Gras


1 Answers

Make sure your APPs (not the database container) are actually using the POSTGRES_USER and POSTGRES_PASSWORD variables. I suspect they are looking for something like DB_USER or similar and so aren't getting the right values in.

By default, every PostgreSQL database driver and admin tool defaults to the postgres user. This may explain why the error message complains about postgres even if the environment variable isn't being used.

A good way to verify is to change all references to the database user in the docker-compose file to something like postgres2. I suspect you'll still see apps complaining that password auth failed for postgres.

like image 91
Miles Elam Avatar answered Oct 12 '22 22:10

Miles Elam