SSL/TLS 활성화 및 암호화 연결 확인(pg_stat_ssl)
PostgreSQL보안명령어
# Server certificate (self-signed for testing; use a corporate CA in production)
openssl req -new -x509 -days 365 -nodes -subj "/CN=db.example.com" -keyout server.key -out server.crt
chmod 600 server.key # in the data directory, owned by postgres
-- Enable SSL (a reload is enough)
ALTER SYSTEM SET ssl = on;
ALTER SYSTEM SET ssl_cert_file = 'server.crt';
ALTER SYSTEM SET ssl_key_file = 'server.key';
ALTER SYSTEM SET ssl_min_protocol_version = 'TLSv1.2';
SELECT pg_reload_conf();
-- Which connections are encrypted?
SELECT a.pid, a.usename, a.client_addr, a.application_name,
s.ssl, s.version, s.cipher, s.bits
FROM pg_stat_ssl s
JOIN pg_stat_activity a USING (pid)
WHERE a.backend_type = 'client backend'
ORDER BY s.ssl, a.usename;
# On the client, verify the server identity as well
psql "host=db.example.com dbname=postgres user=postgres sslmode=verify-full sslrootcert=root.crt"하는 일
재시작 없이 서버 인증서로 TLS를 활성화하고 최소 프로토콜 버전을 설정하며, 현재 연결의 암호화 여부를 TLS 버전 및 cipher와 함께 나열한다.
사용 시점
보안 감사/컴플라이언스 요구 사항(PCI, GDPR), 네트워크로 전송되는 비밀번호와 데이터를 보호하기 위해.
주의
ssl = on만으로는 암호화가 강제되지 않으며 클라이언트는 sslmode=prefer/disable로 여전히 비암호화 연결을 할 수 있다. 강제하려면 pg_hba.conf에서 host 대신 hostssl을 사용한다(hostnossl ... reject도 추가). sslmode=require는 서버 신원을 검증하지 않으므로(MITM에 취약) 인증서 검증에는 verify-full을 사용한다. 키 파일은 postgres 사용자 소유에 모드 0600이어야 한다(또는 root 소유에 0640). 그렇지 않으면 SSL이 로드되지 않으므로 reload 후 로그와 SHOW ssl을 확인한다. 인증서 갱신도 reload로 반영된다.