db 정보를 원하는 값으로 변경했을 때 비밀번호 오류등이 나던 원인은

  db:
    image: postgres:16
    env_file:
      - .env.test
    ports:
      - '5432:5432'
    volumes:
      - ./postgres/user:/var/lib/postgresql/data
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}']
      interval: 5s
      timeout: 5s
      retries: 10
      start_period: 5s

위 코드의 volumes로 인해 남아있던 postgres 내용과 변경하려는 값이 맞지 않아서 생기던 문제로, 해당 폴더를 지우면 에러가 없어졌다.

 

healthcheck에 정보를 보이기 싫어 환경변수로 바꾸려고 했을때 나타나던 user를 찾을 수 없다는 오류와 role 관련 오류는, $ 한개의 경우 healthcheck 내의 환경변수를 가져다 쓰는거라 그 밖의 환경변수를 가져다 쓰려면 $$ 두개로 처리해줘야되었다.

또한, -U 이후 user만 적었을 경우 나오는 role 에러는 원래 -d 로 db 정보를 적어줘야하는데 이를 안적어 줬을 경우 생기는 오류였다. 일반적으로 user와 db를 모두 postgres로 적어놓는 코드들은 안적었을 경우 기본값인 postgres로 인식되어서 문제가 없었지만, 이를 변경하게 된다면 -d 를 통해 해당 값을 적어줘야했던 거다.

 

이를 해결하기 위해 문제를 찾으면서 고생했지만, 일일이 남기자니 너무 길어지고 좀 그래서 핵심만 간단하게 남기는 글로 다시 작성했다.

 

++ 데모사이트의 db를 서버와 함께 묶어서 올릴 생각에 환경변수를 넣어줬지만, 로컬에서 개발용으로 사용할 목적이라면

  db:
    image: postgres:16
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: postgres
    ports:
      - '5432:5432'
    volumes:
      - ./postgres/user:/var/lib/postgresql/data
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U postgres']
      interval: 5s
      timeout: 5s
      retries: 10
      start_period: 5s

이렇게 간단하게 작성하는게 더 좋다.


참고 사이트

https://velog.io/@baeyuna97/Solved-Postgres-and-Docker-Compose-password-authentication-fails-and-role-postgres-does-not-exist

 

[Solved] Postgres and Docker Compose; password authentication fails and role 'postgres' does not exist.

Docker로 Postgres를 구동하고 띄워서 작업을 하고 있다.그러기 위해 Docker-compose.yml 파일을 다음같이 작성하고 진행하였다.environment로 POSTGRES_USER, POSTGRES_PASSWORD, POSTGRES_DB 등을 설정하였

velog.io

 

'코딩 > 문제&에러' 카테고리의 다른 글

AWS 로드밸런서 Health Check Unhealthy  (0) 2024.11.29
Github Action Test 파일 찾을 수 없음  (1) 2024.11.29
pg-mem Nest에 Test DB로 설정하기  (1) 2024.11.16
Test Code의 module  (1) 2024.11.13
TypeORM CASCADE  (0) 2024.11.12

+ Recent posts