PostgreSQL 18 – fdw 연결을 위한 SCRAM 패스스루 인증
Matheus Alcantara
2025년 8월 21일
PostgreSQL 18은 postgres_fdw 또는 dblink_fdw를 사용하는 사용자에게 반가운 기능을 제공합니다: SCRAM 패스스루(pass-through) 인증입니다. 이제 외부 서버(Foreign Server) 연결을 설정할 때 USER MAPPING 옵션에 평문 비밀번호를 저장할 필요가 없습니다.
다음은 이를 가능하게 한 커밋입니다:
commit 761c79508e7fbc33c1b11754bdde4bd03ce9cbb3
Author: Peter Eisentraut <peter@eisentraut.org>
Date: Wed Jan 15 17:55:18 2025 +0100
postgres_fdw: SCRAM authentication pass-through
This enables SCRAM authentication for postgres_fdw when connecting to
a foreign server without having to store a plain-text password on user
mapping options.
This is done by saving the SCRAM ClientKey and ServerKey from the
client authentication and using those instead of the plain-text
password for the server-side SCRAM exchange. The new foreign-server
or user-mapping option "use_scram_passthrough" enables this.
Co-authored-by: Matheus Alcantara <mths.dev@pm.me>
Co-authored-by: Peter Eisentraut <peter@eisentraut.org>
Discussion: https://www.postgresql.org/message-id/flat/27b29a35-9b96-46a9-bc1a-914140869dac@gmail.com
커밋 메시지에서 설명하듯, PostgreSQL 서버가 FOREIGN SERVER에 연결할 때 use_scram_passthrough
옵션을 설정하면, 평문 비밀번호 대신 클라이언트 연결에서 가져온 **SCRAM 키(ClientKey, ServerKey)**를 사용합니다. 이는 보안을 강화하고, 자격 증명 중복 관리 문제를 해결합니다.
사용 조건
이 기능을 활용하려면 다음을 충족해야 합니다:
- 외부 서버가 scram-sha-256 인증을 요구해야 함 (그렇지 않으면 실패)
- 클라이언트 측(Postgres_fdw/dblink_fdw를 사용하는 서버)만 PostgreSQL 18 이상이면 됨
- 양쪽 서버에 동일한 SCRAM 시크릿(해시, salt, iteration count 포함)이 있어야 함
- 최초 클라이언트 → 메인 서버 연결도 SCRAM을 사용해야 함 (즉 “패스스루”는 입·출력 모두 SCRAM 필요)
postgres_fdw로 설정하기
여기서는 두 개의 PostgreSQL 서버를 사용합니다:
- incoming 서버 (fdw 클라이언트)
- foreign 서버 (외부 서버)
1. 두 서버에 동일한 사용자 생성
CREATE USER example;
foreign 서버에서 예제 테이블 생성:
CREATE TABLE fdw_table AS SELECT g as a, b+2 as b FROM generate_series(1,100) g(g);
psql을 종료 후 새 유저로 다시 로그인하여 두 서버에서 비밀번호를 설정합니다:
\password
2. pg_hba.conf 업데이트
두 서버 모두 scram-sha-256 인증을 강제해야 합니다:
local all all scram-sha-256
host all all 127.0.0.1/32 scram-sha-256
host all all ::1/128 scram-sha-256
파일 경로 확인:
SHOW hba_file;
3. 암호 동기화 (SCRAM 시크릿)
incoming 서버에서 암호 해시 가져오기:
SELECT rolpassword FROM pg_authid WHERE rolname = 'example';
foreign 서버에서 동일하게 설정:
ALTER ROLE example PASSWORD 'scram-sha-256$...'; -- paste the whole thing
중요: SCRAM 해시는 양쪽 서버에서 완전히 동일해야 합니다.
4. postgres_fdw 설정
incoming 서버에서 실행:
CREATE EXTENSION IF NOT EXISTS postgres_fdw;
CREATE SERVER foreign_fdw
FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host 'localhost', dbname 'postgres', use_scram_passthrough 'true');
CREATE USER MAPPING FOR example
SERVER foreign_fdw
OPTIONS (user 'example');
👉 주의: USER MAPPING에 비밀번호를 지정할 필요가 없습니다!
5. 외부 테이블 가져오기
IMPORT FOREIGN SCHEMA public LIMIT TO (fdw_table)
FROM SERVER foreign_fdw INTO public;
이제 실행:
SELECT * FROM fdw_table;
💥 성공적으로 SCRAM 패스스루를 통해 서버 간 쿼리 실행!
dblink_fdw는?
설정 절차는 동일합니다. 다만 테이블 가져오기 대신 직접 쿼리를 실행합니다:
SELECT * FROM dblink('foreign_fdw', 'SELECT * FROM fdw_table')
AS fdw_table(a int, b int);
마무리
SCRAM 패스스루 인증은 PostgreSQL 서버 간 보안성 높은 무비밀번호 연결을 가능하게 합니다.
특히 여러 데이터베이스에 연합 접근(federation)을 설정하면서 USER MAPPING 비밀번호를 관리하고 싶지 않은 환경에서 매우 유용합니다.