Docker에 설치된 RockyLinux 8.10 버전에 PostgreSQL 13을 설치 후 데이터 이관 테스트를 진행하려 한다.

Docker에서 PostgreSQL 13을 설치할 때에는 몇가지 추가로 설정해줘야 할 것들이 있어 정리할 겸 작성한다.

 

Docker에서 설치할 때의 에러 및 해결방법을 같이 작성하니, 일반 서버OS(Docker가 아닌)에서는 불필요한 내용도 다소 존재한다.

 

1. PostgreSQL Repository 등록

다음의 명령어로 공식 Repository를 등록한다.

yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm

[root@93e37f9f73ea pgsql]# yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm

PostgreSQL common RPMs for RHEL / CentOS 8 - x86_64                  2.3 MB/s | 2.4 kB     00:00
PostgreSQL common RPMs for RHEL / CentOS 8 - x86_64                  560  B/s | 659  B     00:01
PostgreSQL common RPMs for RHEL / CentOS 8 - x86_64                  2.3 MB/s | 2.4 kB     00:00
PostgreSQL common RPMs for RHEL / CentOS 8 - x86_64                  552  B/s | 659  B     00:01
Error: Failed to download metadata for repo 'pgdg-common': repomd.xml GPG signature verification error: Bad GPG signature

위 오류는 GPG 서명검증 실패 오류로, 다음의 명령어로 재시도한다.

 

yum install -y --nogpgcheck https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm

[root@93e37f9f73ea pgsql]# yum install -y --nogpgcheck https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm

PostgreSQL common RPMs for RHEL / CentOS 8 - x86_64                  244 kB/s | 468 kB     00:01
pgdg-redhat-repo-latest.noarch.rpm                                    20 kB/s |  15 kB     00:00
Dependencies resolved.
=====================================================================================================
 Package                     Architecture      Version                 Repository               Size
=====================================================================================================
Upgrading:
 pgdg-redhat-repo            noarch            42.0-43PGDG             @commandline             15 k

Transaction Summary
========================================================================================================================
Install  1 Package

Total size: 15 k
Installed size: 15 k
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                1/1
  Installing       : pgdg-redhat-repo-42.0-43PGDG.noarch                                                            1/1
  Verifying        : pgdg-redhat-repo-42.0-43PGDG.noarch                                                            1/1

Installed:
  pgdg-redhat-repo-42.0-43PGDG.noarch

Complete!

 

2. 기본 PostgreSQL 모듈 비활성화

RockyLinux 8.10 버전에서는 기본 PostgreSQL 모듈 활성화가 되어있으니, 비활성화 작업을 거친다.

이 작업은 약간의 시간을 소요한다.

 

yum module disable -y postgresql

[root@82559cbe0973 /]# yum module disable -y postgresql

PostgreSQL common RPMs for RHEL / Rocky / AlmaLinux 8 - x86_64                          364  B/s | 659  B     00:01
PostgreSQL common RPMs for RHEL / Rocky / AlmaLinux 8 - x86_64                          2.4 MB/s | 2.4 kB     00:00
Importing GPG key 0x08B40D20:

(중략)

Dependencies resolved.
========================================================================================================================
 Package                     Architecture               Version                       Repository                   Size
========================================================================================================================
Disabling modules:
 postgresql

Transaction Summary
========================================================================================================================

Complete!

 

3. PostgreSQL 13 Server버전 설치

이제 PostgreSQL 13 Server를 설치한다.

 

yum install -y postgresql13 postgresql13-server

[root@82559cbe0973 /]# yum install -y postgresql13 postgresql13-server

Last metadata expiration check: 0:00:42 ago on Wed Aug 21 02:01:09 2024.
Dependencies resolved.
========================================================================================================================
 Package                            Architecture          Version                           Repository             Size
========================================================================================================================
Installing:
 postgresql13                       x86_64                13.16-2PGDG.rhel8                 pgdg13                1.5 M
 postgresql13-server                x86_64                13.16-2PGDG.rhel8                 pgdg13                5.5 M
Installing dependencies:
 libicu                             x86_64                60.3-2.el8_1                      baseos                8.8 M
 postgresql13-libs                  x86_64                13.16-2PGDG.rhel8                 pgdg13                420 k

Transaction Summary
========================================================================================================================
Install  4 Packages

(중략)

Installed:
  libicu-60.3-2.el8_1.x86_64                                postgresql13-13.16-2PGDG.rhel8.x86_64
  postgresql13-libs-13.16-2PGDG.rhel8.x86_64                postgresql13-server-13.16-2PGDG.rhel8.x86_64

Complete!

 

4. PostgreSQL 설정 및 구동

PostgreSQL DB 초기화 및 권한과 관련해서 설정을 진행해주어야 한다. 다음의 명령어를 수행한다.

 

4-1. PostgreSQL DB 초기화

해당 디렉토리가 존재하지 않는다면 해당 디렉토리를 생성한다.

존재한다면 이 단계는 건너뛰어도 된다.

[root@82559cbe0973 bin]# mkdir /var/lib/pgsql/13/data
[root@82559cbe0973 bin]# cd /var/lib/pgsql/13/data
[root@82559cbe0973 data]# pwd
/var/lib/pgsql/13/data

 

4-2. postgres 계정 권한부여 및 계정전환

PostgreSQL 13 을 설치하면 postgres 라는 계정이 생성된다. 해당 계정으로 PostgreSQL13을 구동하는 등, PostgreSQL 기능과 관련된 여러 동작을 수행한다.

해당 계정에 위 4-1. 디렉토리의 접근권한을 부여해준 후, 해당 계정으로 접속한다.

[root@93e37f9f73ea var]# chown postgres:postgres /var/lib/pgsql/13/data
[root@93e37f9f73ea var]#
[root@93e37f9f73ea var]# su - postgres

 

4-3. DB 클러스터 초기화

postgres 계정으로 아래의 명령어를 수행하여 DB 클러스터를 초기화해준다.

[postgres@93e37f9f73ea ~]$ /usr/pgsql-13/bin/initdb -D /var/lib/pgsql/13/data

The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "C.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/pgsql/13/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Etc/UTC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    /usr/pgsql-13/bin/pg_ctl -D /var/lib/pgsql/13/data -l logfile start

 

4-4. PostgreSQL 실행 및 중지

아래의 명령어를 통해 실행 또는 중지한다.

/usr/pgsql-13/bin/pg_ctl -D /var/lib/pgsql/13/data start 

/usr/pgsql-13/bin/pg_ctl -D /var/lib/pgsql/13/data stop

로그는 /var/lib/pgsql/13/data/log 경로에 위치해있다.

 

5. 호스트 PC에서 접속하기 위한 설정

Docker에서 PostgreSQL을 구동시켰으니 호스트PC인 Windows 에서 DBEaver를 통해 접속하려 한다. 꽤 많은(?) 삽질이 필요했는데, 아래의 방법을 진행하여 연결에 성공했다.

 

5-1. 접속 포트 설정

현재 Docker는 18081 포트로 포트포워딩이 설정된 상태이다. postgreSQL은 5432 포트가 기본포트이므로, 아래의 설정파일을 편집한다. 모든 과정은 postgres 계정으로 진행한다.

아래 항목 중, listen_address 항목을 모든 IP주소인 '*' 로 설정하고, port는 5432 에서 변경할 포트인 18081로 수정한다.

기본적으로 주석처리가 되어있으므로 주석 또한 해제한다.

 

postgresql 서버를 재시작해야 설정이 적용된다.

[postgres@82559cbe0973 data] cd /var/lib/pgsql/13/data
[postgres@82559cbe0973 data] vi postgresql.conf

(중략)

# - 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)
port = 18081                            # (change requires restart)

 

5-2. 외부 접근 허용 정책 설정

외부 접근을 허용하는 정책을 설정하기 위해 같은 디렉토리에 있는 pg_hba.conf 파일을 열어준다.

최하단의 볼드체로 표시된 항목 두 개를 추가한다.

 

postgresql 서버를 재시작해야 설정이 적용된다.

[postgres@82559cbe0973 data] cd /var/lib/pgsql/13/data
[postgres@82559cbe0973 data] vi pg_hba.conf

(중략) 

# replication privilege.
local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trust
host    all     all             0.0.0.0/0               md5
host    all     all             ::/0                    md5

 

5-3. 기본계정 postgres 비밀번호 설정

어디에선가 본 것으로는 postgres 계정의 비밀번호는 postgres라던데 사실 아니었나보다 -_-;

그래서 직접 접속 후, postgres 계정의 비밀번호를 설정해준다.

 

위 5-1. 항목에서 5432 포트를 18081로 변경하였기 때문에, 기본 접속 명령어인 psql  명령어가 오류가 나게 된다.

이럴 때에는 당황하지 말고 변경한 포트설정 옵션을 넣어주어야 한다.

(오류 예시)
[postgres@82559cbe0973 data]$ psql
psql: error: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/run/postgresql/.s.PGSQL.5432"?

 

(포트변경 후 옵션 설정)
[postgres@82559cbe0973 data]$ psql -h localhost -p 18081
psql (13.16)
Type "help" for help.

postgres=#

 

접속이 되었다면 아래의 쿼리를 입력하여 비밀번호를 설정해주자.

postgres=# alter user postgres password 'test1!';
ALTER ROLE

 

그리고 DBEaver 에서 접속하면 정상적으로 접속됨을 확인할 수 있다.

 

DB를 로컬 Windows PC에는 많이 설치해보고 개발용으로 설정한 적은 많아도, Docker Linux에 세팅한 후 접속해보는 건 처음인것 같다. 여러모로 설정이 많아 헷갈리기도 하고 조금 많이 어렵기도 했다. 실제 Linux 서버에 설치하는건 조금 다를것이고, 보안을 고려한다면 외부 접속 설정 정책 부분도 손을 좀 더 봐야 할 것으로 예상된다. 뭐, 그래도 해본게 어딘가 싶긴 하지만 말이다.

블로그 이미지

김생선

세상의 모든것을 어장관리

,