IT/DB

[DB] postgresql 설치

saipe 2023. 6. 21. 17:08
반응형

개요) 처음 사용해보는 DB로 설치 방법을 메모해두기 위함

- CentOS 7 에서 yum 으로 postgresql 설치 (9.2)

- 상세한 설정 등은 별도 글을 참고하자 (설치 후 기본 구성만 해두는게 이번 포스팅의 목적)

 

※ 설치요약

# yum 설치

# 초기 DB 데이터 설치 (postgres-setup initdb)

# 서비스 기동 및 자동기동 구성

 

 

 

1) postgresql 설치 (yum)

yum install postgresql-server -y

- 당연히 인터넷이 연결되어 있어야 함

- 설치 후 postgresql 용 계정이 설치됨 (postgres)

[root@c79 ~]# cat /etc/passwd | grep postgres
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash

 

2) DB 데이터 저장경로

- 최초 설치 후 기본 데이터 경로는 DB 설정(홈디렉토리\postgresql.conf   >   #data_directory = 'ConfigDir')이 정의되어 있지 않아 'PGDATA' 변수값으로 참조됨 (홈디렉토리\.bash_profile)
- 기본 데이터 설치 진행 (postgres-setup initdb)

경로 변경이 없다면 /var/lib/pgsql/data 에 설치됨

▷ 당연히 기본 경로가 아닌 경우 실행시 -D 옵션으로 경로를 지정해 주거나, 환경 설정에 변경 디렉토리를 기입해 주어야 함

[root@c79 ~]# cat /var/lib/pgsql/.bash_profile 
[ -f /etc/profile ] && source /etc/profile

PGDATA=/var/lib/pgsql/data
export PGDATA
[root@c79 ~]# ll /var/lib/pgsql/data
total 0
[root@c79 ~]# ll /var/lib/pgsql/data
total 0

[root@c79 ~]# postgresql-setup initdb
Initializing database ... OK

[root@c79 ~]# ll /var/lib/pgsql/data/
total 88
drwx------. 5 postgres postgres  4096 Jun 21 10:07 base
drwx------. 2 postgres postgres  4096 Jun 21 10:07 global
drwx------. 2 postgres postgres  4096 Jun 21 10:07 pg_clog
-rw-------. 1 postgres postgres  4232 Jun 21 10:07 pg_hba.conf
-rw-------. 1 postgres postgres  1636 Jun 21 10:07 pg_ident.conf
drwx------. 2 postgres postgres  4096 Jun 21 10:07 pg_log
drwx------. 4 postgres postgres  4096 Jun 21 10:07 pg_multixact
drwx------. 2 postgres postgres  4096 Jun 21 10:07 pg_notify
drwx------. 2 postgres postgres  4096 Jun 21 10:07 pg_serial
drwx------. 2 postgres postgres  4096 Jun 21 10:07 pg_snapshots
drwx------. 2 postgres postgres  4096 Jun 21 10:07 pg_stat_tmp
drwx------. 2 postgres postgres  4096 Jun 21 10:07 pg_subtrans
drwx------. 2 postgres postgres  4096 Jun 21 10:07 pg_tblspc
drwx------. 2 postgres postgres  4096 Jun 21 10:07 pg_twophase
-rw-------. 1 postgres postgres     4 Jun 21 10:07 PG_VERSION
drwx------. 3 postgres postgres  4096 Jun 21 10:07 pg_xlog
-rw-------. 1 postgres postgres 19845 Jun 21 10:07 postgresql.conf

 

3) 기동

- 서비스가 열려 있나 확인 (5432 포트)

 

systemctl start postgresql

또는

(postgres 계정) pg_ctl start

[root@c79 ~]# systemctl start postgresql
[root@c79 ~]# ss -lntp | grep 5432
LISTEN     0      128    127.0.0.1:5432                     *:*                   users:(("postgres",pid=1711,fd=4))
LISTEN     0      128      [::1]:5432                  [::]:*                   users:(("postgres",pid=1711,fd=3))

- 부팅시 자동 기동되도록 설정

systemctl enable postgresql


4) 접속하기 (로컬)

[root@c79 data]# su - postgres
Last login: Wed Jun 21 16:05:36 KST 2023 on pts/0
-bash-4.2$ psql
psql (9.2.24)
Type "help" for help.

postgres=#

 

 

 

아래는 부가적인 내용으로 외부 접속 및 계정 생성과 DB 생성 등에 대해 간단히 적어둠

 

#1) DB 생성

 

create database [DB명];

 

postgres=# create database test_db;
CREATE DATABASE
postgres=# \l
                                  List of databases
    Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
------------+----------+----------+-------------+-------------+-----------------------
 postgres   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
            |          |          |             |             | postgres=CTc/postgres
 template1  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
            |          |          |             |             | postgres=CTc/postgres
 test_db    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |

- test_db 라는 DB가 생성됨

 

#2) 계정 생성


CREATE USER [계정명] WITH PASSWORD '[패스워드]';

 

postgres=# create user imuser with password 'test1234';
CREATE ROLE
postgres=# \du
                             List of roles
 Role name |                   Attributes                   | Member of 
-----------+------------------------------------------------+-----------
 imuser    |                                                | {}
 postgres  | Superuser, Create role, Create DB, Replication | {}

- 계정 생성 후 확인

 

#3) DB에 계정접근권한 부여

 

GRANT ALL PRIVILEGES ON DATABASE [DB명] TO [계정명];

 

postgres=# GRANT ALL PRIVILEGES ON DATABASE test_db TO imuser;
GRANT
postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 test_db   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =Tc/postgres         +
           |          |          |             |             | postgres=CTc/postgres+
           |          |          |             |             | imuser=CTc/postgres
(4 rows)

 

#4) 외부접속 허용

- 최초 구성은 로컬 접속만 허용되어 외부 접속을 열어줘야 함 (서비스 재시작 필요)

- (데이터)\pg_hba.conf 수정

vi /var/lib/pgsql/data/postgresql.conf

 

- listen_address 부분을 아래와 같이 설정

#listen_addresses = 'localhost'
>
listen_addresses = '*'

 

- 설정 변경 후 DB 재기동

 

systemctl restart postgresql

또는

(postgres 계정) pg_ctl restart

[root@c79 ~]# vi /var/lib/pgsql/data/postgresql.conf
[root@c79 ~]# ss -lntp | grep 5432
LISTEN     0      128    127.0.0.1:5432                     *:*                   users:(("postgres",pid=1711,fd=4))
LISTEN     0      128      [::1]:5432                  [::]:*                   users:(("postgres",pid=1711,fd=3))
[root@c79 ~]# systemctl restart postgresql
[root@c79 ~]# ss -lntp | grep 5432
LISTEN     0      128          *:5432                     *:*                   users:(("postgres",pid=1769,fd=3))
LISTEN     0      128       [::]:5432                  [::]:*                   users:(("postgres",pid=1769,fd=4))

- 설정 변경 후 로컬 허용만 되어 있던게 외부 접속이 가능해짐

- 외부 접속은 가능하나, 접근제어가 로컬만 설정되어 있기에 접근제어 리스트를 수정해야 함 (pg_hba.conf)

- cat /var/lib/pgsql/data/pg_hba.comf

...
(내용들)
...

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
# IPv6 local connections:
host    all             all             ::1/128                 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            ident
#host    replication     postgres        ::1/128                 ident

- 위의 분류되로 우리가 접속할 접근제어 리스트를 추가하자

▷ TYPE : host (호스트)

DATABASE : all (어떤 DB에 접근이 가능한지, 모든 DB는 all)

USER : all (접근할 계정명, 모든 유저는 all)

ADDRESS : 0.0.0.0/0 (접근할 IP들, 모든 접근 허용은 0.0.0.0/0)

METHOD : MD5 (다른값 peer, ident 로 하면 접속 오류 나니 MD5로 할 것)

host    test_db           imuser             192.168.110.0/24        md5

- 설정 반영은 역시 다시 읽어오거나 재기동 해야 됨

 

(postgres 계정) pg_ctl reload

 

그리고 접근 테스트를 해보자

 DB 툴 등을 이용 (DBeaver)

 다른 리눅스에서 접근) psql -U (계정명) -d (DB명) -h (DB서버 주소)

 

- 접근이 안되면 방화벽 (5432포트), listen_address, METHOD (MD5) 등을 확인해보자

 


업데이트) 2023.06.21

반응형