[PostgreSQL] 원격 접속 허용 방법 (외부 접근 제어, 접속 인증 설정)
PostgreSQL 원격 접속 허용 방법
PostgreSQL를 처음 설치하고, 초기 설정값으로 설정된 데이터베이스를 외부에서 접속 시 아래와 같은 에러가 나타나며 접속이 되지 않습니다.
이는 PostgreSQL DB에 접속하는 클라이언트에 대한 접근제어 및 인증 등을 설정하는 파일인 pg_hba.conf 때문인데요.
초기 pg_hba.conf 설정은 아래와 같습니다. (pg_hba.conf 파일은 $PGDATA 경로안에 존재)
외부에서 PostgreSQL로 접근이 가능하게 하려면 다음과 같이 설정하면 됩니다.
pg_hba.conf 설정이 끝났으면, 데이터베이스에 pg_hba.conf 변경내용을 적용하기 위해 reload 합니다.
$ /postgres/app/postgres/pgsql15/bin/pg_ctl reload -D /postgres/app/postgres/pgsql15/data
만약, pg_hba.conf 파일내용만 수정하고 외부에서 접속테스트를 시도하시면 아래와 같이 Connection refused: connect 에러가 나오면서 접속이 되지 않으실겁니다.
한가지를 더 추가로 작업해주셔야 외부원격접속이 가능한데, 바로 postgresql.conf 파일 내용 중 listen_addresses 파라미터 내용을 아래와 같이 수정해주시면 됩니다. (postgresql.conf 파일은 $PGDATA 경로안에 존재)
listen_addresses 변경 전
$ vi postgresql.conf
...(생략)...
#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------
# - Connection Settings -
listen_addresses = 'localhost' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
...(생략)...
listen_addresses 변경 후
$ vi postgresql.conf
...(생략)...
#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------
# - Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
...(생략)...
해당 파라미터는 재기동을 해야 적용이 되므로 DB restart를 진행해줍니다.
$ /postgres/app/postgres/pgsql15/bin/pg_ctl restart -D /postgres/app/postgres/pgsql15/data
이제 외부에서 접속이 잘 될겁니다!