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

md5 비밀번호에서 scram-sha-256으로 전환

PostgreSQL보안

명령어

-- 1) Store new passwords as SCRAM
ALTER SYSTEM SET password_encryption = 'scram-sha-256';
SELECT pg_reload_conf();

-- 2) Find roles still carrying md5 password hashes (requires superuser)
SELECT rolname,
       CASE
         WHEN rolpassword LIKE 'SCRAM-SHA-256$%' THEN 'scram-sha-256'
         WHEN rolpassword LIKE 'md5%' THEN 'md5'
         WHEN rolpassword IS NULL THEN 'no password'
         ELSE 'other'
       END AS password_type,
       rolvaliduntil
FROM pg_authid
WHERE rolcanlogin
ORDER BY password_type, rolname;

-- 3) Reset the password of each md5 role (preferably in psql: \password legacy_app)
ALTER ROLE legacy_app PASSWORD 'New#Password2';

# 4) Once all roles use SCRAM, change md5 -> scram-sha-256 in pg_hba.conf and reload

하는 일

새 비밀번호가 SCRAM-SHA-256으로 저장되게 하고, 아직 md5 해시를 가진 로그인 롤을 찾으며, 비밀번호 갱신 후 pg_hba.conf를 scram-sha-256으로 전환하는 단계를 제공한다.

사용 시점

보안 감사에서 md5가 지적될 때, PG14+로 업그레이드한 후, md5가 deprecated된 버전을 준비할 때.

주의

기존 md5 해시에서 SCRAM을 도출할 수 없으므로 모든 비밀번호를 평문으로 다시 설정해야 한다(애플리케이션 팀과 조율 필요). pg_hba.conf의 'md5' 방식은 비밀번호가 SCRAM인 롤에 대해 자동으로 SCRAM 인증을 수행하므로 전환 중 md5를 유지하는 것은 안전한 중간 단계다. 그러나 방식이 'scram-sha-256'이 되면 md5 비밀번호를 가진 롤은 더 이상 연결할 수 없다. 오래된 클라이언트 드라이버(libpq < 10, 오래된 JDBC/.NET 드라이버)는 SCRAM을 지원하지 않을 수 있으므로 먼저 드라이버를 업그레이드한다. ALTER ROLE ... PASSWORD는 log_statement를 통해 비밀번호를 로그에 기록할 수 있다. psql \password는 전송 전에 클라이언트에서 해시한다.

DBAtlas에서 열기 →

관련 항목