본문 바로가기

Database

[Postgresql] 외부에서 접속하기

1. 포트 확인

root@django:/home/ivan# netstat -ntlp | grep 5432
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      1481/postgres

  - DB에서 사용하는 포트 = 5432

  - 127.0.0.1:5432 -> 내부에서만 사용 가능

 

 

2. Config 변경

  - postgresql.conf 파일 변경

root@django:/etc/postgresql/10/main# ls -al postgresql.conf
-rw-r--r-- 1 postgres postgres 23045 Nov 12 23:13 postgresql.conf
root@django:/etc/postgresql/10/main# 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)

    > listen_addresses = 'localhost'에서 변경

 

 

  - DB restart

root@django:/etc/postgresql/10/main# /etc/init.d/postgresql restart
[ ok ] Restarting postgresql (via systemctl): postgresql.service.
root@django:/etc/postgresql/10/main#

 

 

3. 포트 재확인

root@django:/etc/postgresql/10/main# netstat -ntlp|grep 5432
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN      13782/postgres
tcp6       0      0 :::5432                 :::*                    LISTEN      13782/postgres

 

 

4. 권한 부여

root@django:/etc/postgresql/10/main# vi pg_hba.conf
# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             0.0.0.0/0            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5

 

  - DB restart

root@django:/etc/postgresql/10/main# /etc/init.d/postgresql restart
[ ok ] Restarting postgresql (via systemctl): postgresql.service.
root@django:/etc/postgresql/10/main#

 

 

5. 외부 접속 확인