💾Data/🧩Database

PostgreSQL 외부접속 설정

공개하기부끄러운블로그 2021. 1. 27. 15:07
반응형
OS : CentOS7.9
DB : PostgreSQL9.6

 

[root@localhost ~]# su - postgres 
-bash-4.2$ psql 
psql (9.6.20) 
Type "help" for help. 

# doit계정 생성
postgres=# create user doit superuser; 
CREATE ROLE 

# doit계정 Password 설정
postgres=# alter user doit with password '123123'; 
ALTER ROLE 

# doitDB생성 및 doit계정에게 권한 부여
postgres=# create database doitDB with owner doit encoding 'UTF8';
CREATE DATABASE

# 생성된 DB확인 (doitDB)
postgres=# \l
                                 데이터베이스 목록
   이름    |  소유주  | 인코딩 |   Collate   |    Ctype    |      액세스 권한      
-----------+----------+--------+-------------+-------------+-----------------------
 doitdb    | doit     | UTF8   | ko_KR.UTF-8 | ko_KR.UTF-8 | 
 postgres  | postgres | UTF8   | ko_KR.UTF-8 | ko_KR.UTF-8 | 
 template0 | postgres | UTF8   | ko_KR.UTF-8 | ko_KR.UTF-8 | =c/postgres          +
           |          |        |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8   | ko_KR.UTF-8 | ko_KR.UTF-8 | =c/postgres          +
           |          |        |             |             | postgres=CTc/postgres
(4개 행)

postgres=# pg_ctl reload

 

외부 접속 허용 전 config 파일 확인 후 작업
postgres=# show hba_file;
              hba_file               
-------------------------------------
 /var/lib/pgsql/9.6/data/pg_hba.conf
(1개 행)

# [root@localhost ~]# su - postgres

# -bash-4.2$ vim 9.6/data/pg_hba.conf     // 아래 와 같이 한줄추가

# -bash-4.2$ vim 9.6/data/postgresql.conf     // 주석 해제 후 listen_addresses 수정

 

root로 전환 후 PostgreSQL Restart

[root@localhost ~]# systemctl restart postgresql-9.6

 

[root@localhost ~]# netstat -nltp

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1021/sshd           
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN      16981/postmaster    
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1182/master         
tcp6       0      0 :::22                   :::*                    LISTEN      1021/sshd           
tcp6       0      0 :::5432                 :::*                    LISTEN      16981/postmaster    
tcp6       0      0 ::1:25                  :::*                    LISTEN      1182/master   

 

이후에도 외부에서 접근에 문제가 된다면 방화벽을 확인해봐야한다.

PostgreSQL의 Default Port는 5432/tcp 이다.

반응형