DB

PostgreSQL 외부 접속 허용 및 IP주소 제한

땅부자몽구스 2021. 11. 7. 00:23

클라우드 인스턴스에서 PostgreSQL의 포트를 열어놓고 외부 접속을 허용했더니,

디도스 비슷한 트래픽 공격으로 서버 메모리가 치솟은 경험이 있다.

 

이 때문에 외부 접속은 허용하되,

IP주소로 제한을 걸어서 특정 사용자만 외부에서 접근할 수 있도록 정책을 바꾸었다.

해당 설정 방법에 대해 공유한다.

 

요약부터 하면,

postgresql.conf 파일에서 외부 모든 아이피의 접속을 허용하고,
pg_hba.conf 파일에서 허용할 아이피를 제한한다.
이게 일반적인 설정 방법이다.

 

먼저 pg_hba.conf 파일을 열어서 수정한다.

(파일 위치: /etc/postgresql/버전/main/pg_hba.conf)

vim /etc/postgresql/버전/main/pg_hba.conf

 

문서 제일 하단 IPv4 local connections 부분에 허용하려는 IP 주소를 추가한다.

# "local" is for Unix domain socket connections only
local    all    all    peer
# IPv4 local connections:
host    all    all    127.0.0.1/32    md5
# 여기에 추가
host    all    all    추가할 ip 주소1  md5
host    all    all    추가할 ip 주소2  md5
...

 

다음으로 postgresql.conf 파일에서 외부 접속을 허용한다.

(파일 위치: /etc/postgresql/버전/main/postgresql.conf)

vim /etc/postgresql/버전/main/postgresql.conf

 

listen_addresses를 *로 설정한다.

...
# CONNECTIONS AND AUTHENTICATION
...

# - Connection Settings -

listen_addresses = '*'

...

 

참고자료

https://cloud.google.com/community/tutorials/setting-up-postgres

 

Set up PostgreSQL on Compute Engine  |  Google Cloud Platform Community

[{ "type": "thumb-down", "id": "hardToUnderstand", "label":"Hard to understand" },{ "type": "thumb-down", "id": "incorrectInformationOrSampleCode", "label":"Incorrect information or sample code" },{ "type": "thumb-down", "id": "missingTheInformationSamples

cloud.google.com