DBAtlas · 데이터베이스 운영 아틀라스

pg_hba.conf 규칙 순서 검증 및 안전한 reload

PostgreSQL보안

명령어

-- File location
SHOW hba_file;

# Rules are read top to bottom and the FIRST matching rule wins;
# specific rules must come BEFORE general ones:
#   hostssl  appdb  web_app  10.10.0.0/16  scram-sha-256
#   host     all    all      10.10.0.0/16  reject
#   host     all    all      0.0.0.0/0     scram-sha-256

-- Validate BEFORE reloading: pg_hba_file_rules reads the file on disk
SELECT line_number, type, database, user_name, address, netmask, auth_method, error
FROM pg_hba_file_rules
ORDER BY line_number;

-- Apply if the error column is empty
SELECT pg_reload_conf();

하는 일

reload 전에 pg_hba.conf의 위치, PostgreSQL이 이를 해석하는 방식(줄, type, database, user, address, method), 구문 오류를 보여 준 후 구성을 reload한다.

사용 시점

새 클라이언트 네트워크/애플리케이션을 추가할 때, md5에서 scram-sha-256으로 전환할 때, 'no pg_hba.conf entry for host' 오류를 조사할 때.

주의

pg_hba_file_rules는 활성 규칙이 아니라 현재 파일 상태를 보여 준다. 어느 줄이라도 error가 비어 있지 않으면 reload 시 pg_hba.conf를 전혀 로드하지 않으며 이전 규칙이 유지된다(로그에 'pg_hba.conf was not reloaded'). 변경은 새 연결에만 적용된다. 앞쪽에 놓인 넓은 규칙(예: host all all 0.0.0.0/0 trust)은 그 뒤의 모든 제한을 무력화한다. SSL이 꺼져 있으면 hostssl 줄은 매칭될 수 없으며 error 컬럼에 메시지가 표시된다. 기본적으로 superuser만 뷰를 읽을 수 있다. root로 파일을 편집하는 도구가 소유권을 바꾸지 않도록 한다. postgres 사용자가 읽을 수 없으면 reload가 실패한다.

DBAtlas에서 열기 →

관련 항목