PostgreSQL CentOS8 설치 및 시작 방법

CentOS 8에서 PostgreSQL을 설치하는 방법을 알아보겠습니다.

CentOS 8에서는 Yum 후속 버전인 DNF를 사용해 패키지 모듈을 관리합니다.

Yum을 사용해 PostgreSQL를 설치하려고 하면 에러가 발생합니다.

[root@test ~]# yum -y install postgresql14-server
Last metadata expiration check: 2:52:00 ago on Tue 05 Dec 2023 01:56:04 PM JST.
No match for argument: postgresql14-server
Error: Unable to find a match: postgresql14-server

 

PostgreSQL 모듈 확인

CentOS 8에서는 dnf 명령어를 사용해 PostgreSQL를 설치해야합니다.

하지만 dnf 명령어를 사용해 postgresql14-server패키지를 설치하려고 하면 CentOS 8 표준 postgresql모듈이 설정되어 있기 때문에 오류가 발생합니다.

dnf module list postgresql 커맨드를 사용해 표준 postgresql모듈을 확인해보겠습니다.

[root@test ~]# dnf module list postgresql
Waiting for process with pid 6199 to finish.
Last metadata expiration check: 0:00:10 ago on Thu 14 Dec 2023 02:16:02 PM JST.
Oracle Linux 8 Application Stream (x86_64)
Name         Stream   Profiles             Summary
postgresql   9.6      client, server [d]   PostgreSQL server and client module
postgresql   10 [d]   client, server [d]   PostgreSQL server and client module
postgresql   12       client, server [d]   PostgreSQL server and client module
postgresql   13       client, server [d]   PostgreSQL server and client module
postgresql   15       client, server [d]   PostgreSQL server and client module

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

 

postgresql14 RPM 인스톨

postgresql14를 설치하기 위해 먼저 리포지토리 RPM을 설치합니다.

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

[root@test ~]# dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
Last metadata expiration check: 0:13:23 ago on Thu 14 Dec 2023 02:16:02 PM JST.
pgdg-redhat-repo-latest.noarch.rpm              9.3 kB/s |  13 kB     00:01
Dependencies resolved.
================================================================================
 Package                Architecture Version           Repository          Size
================================================================================
Installing:
 pgdg-redhat-repo       noarch       42.0-35PGDG       @commandline        13 k

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

Total size: 13 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-35PGDG.noarch                    1/1
  Verifying        : pgdg-redhat-repo-42.0-35PGDG.noarch                    1/1

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

Complete!

 

CentOS 표준 PostgreSQL 무효화

표준 postgresql를 무효화를 해야합니다.

dnf -y module disable postgresql

[root@test ~]# dnf -y module disable postgresql
PostgreSQL common RPMs for RHEL / Rocky 8 - x86 107  B/s | 195  B     00:01
PostgreSQL common RPMs for RHEL / Rocky 8 - x86 1.6 MB/s | 1.7 kB     00:00
Importing GPG key 0x442DF0F8:
 Userid     : "PostgreSQL RPM Building Project <pgsql-pkg-yum@postgresql.org>"
 Fingerprint: 68C9 E2B9 1A37 D136 FE74 D176 1F16 D2E1 442D F0F8
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
PostgreSQL common RPMs for RHEL / Rocky 8 - x86 169 kB/s | 790 kB     00:04
PostgreSQL 16 for RHEL / Rocky 8 - x86_64       103  B/s | 195  B     00:01
PostgreSQL 16 for RHEL / Rocky 8 - x86_64       1.6 MB/s | 1.7 kB     00:00
Importing GPG key 0x442DF0F8:
 Userid     : "PostgreSQL RPM Building Project <pgsql-pkg-yum@postgresql.org>"
 Fingerprint: 68C9 E2B9 1A37 D136 FE74 D176 1F16 D2E1 442D F0F8
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
PostgreSQL 16 for RHEL / Rocky 8 - x86_64        75 kB/s | 230 kB     00:03
PostgreSQL 15 for RHEL / Rocky 8 - x86_64       103  B/s | 195  B     00:01
PostgreSQL 15 for RHEL / Rocky 8 - x86_64       1.6 MB/s | 1.7 kB     00:00
Importing GPG key 0x442DF0F8:
 Userid     : "PostgreSQL RPM Building Project <pgsql-pkg-yum@postgresql.org>"
 Fingerprint: 68C9 E2B9 1A37 D136 FE74 D176 1F16 D2E1 442D F0F8
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
PostgreSQL 15 for RHEL / Rocky 8 - x86_64       135 kB/s | 473 kB     00:03
PostgreSQL 14 for RHEL / Rocky 8 - x86_64       122  B/s | 195  B     00:01
PostgreSQL 14 for RHEL / Rocky 8 - x86_64       814 kB/s | 1.7 kB     00:00
Importing GPG key 0x442DF0F8:
 Userid     : "PostgreSQL RPM Building Project <pgsql-pkg-yum@postgresql.org>"
 Fingerprint: 68C9 E2B9 1A37 D136 FE74 D176 1F16 D2E1 442D F0F8
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
PostgreSQL 14 for RHEL / Rocky 8 - x86_64       230 kB/s | 774 kB     00:03
PostgreSQL 13 for RHEL / Rocky 8 - x86_64       123  B/s | 195  B     00:01
PostgreSQL 13 for RHEL / Rocky 8 - x86_64       217 kB/s | 1.7 kB     00:00
Importing GPG key 0x442DF0F8:
 Userid     : "PostgreSQL RPM Building Project <pgsql-pkg-yum@postgresql.org>"
 Fingerprint: 68C9 E2B9 1A37 D136 FE74 D176 1F16 D2E1 442D F0F8
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
PostgreSQL 13 for RHEL / Rocky 8 - x86_64       234 kB/s | 1.0 MB     00:04
PostgreSQL 12 for RHEL / Rocky 8 - x86_64       109  B/s | 195  B     00:01
PostgreSQL 12 for RHEL / Rocky 8 - x86_64       965 kB/s | 1.7 kB     00:00
Importing GPG key 0x442DF0F8:
 Userid     : "PostgreSQL RPM Building Project <pgsql-pkg-yum@postgresql.org>"
 Fingerprint: 68C9 E2B9 1A37 D136 FE74 D176 1F16 D2E1 442D F0F8
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
PostgreSQL 12 for RHEL / Rocky 8 - x86_64       234 kB/s | 1.1 MB     00:05
PostgreSQL 11 for RHEL / Rocky 8 - x86_64        98  B/s | 195  B     00:01
PostgreSQL 11 for RHEL / Rocky 8 - x86_64       1.4 MB/s | 1.7 kB     00:00
Importing GPG key 0x442DF0F8:
 Userid     : "PostgreSQL RPM Building Project <pgsql-pkg-yum@postgresql.org>"
 Fingerprint: 68C9 E2B9 1A37 D136 FE74 D176 1F16 D2E1 442D F0F8
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
PostgreSQL 11 for RHEL / Rocky 8 - x86_64       240 kB/s | 1.3 MB     00:05
Last metadata expiration check: 0:00:01 ago on Thu 14 Dec 2023 02:34:04 PM JST.
Dependencies resolved.
================================================================================
 Package           Architecture     Version             Repository         Size
================================================================================
Disabling modules:
 postgresql

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

Complete!

 

무효화가 완료되었습니다.

설치 가능한 postgresql 버전을 확인해보겠습니다.

dnf search postgresql*-server

[root@test ~]# dnf search postgresql*-server
Last metadata expiration check: 0:03:01 ago on Thu 14 Dec 2023 02:34:04 PM JST.
======================= Name Matched: postgresql*-server =======================
postgresql11-server.x86_64 : The programs needed to create and run a PostgreSQL
                           : server
postgresql12-server.x86_64 : The programs needed to create and run a PostgreSQL
                           : server
postgresql13-server.x86_64 : The programs needed to create and run a PostgreSQL
                           : server
postgresql14-server.x86_64 : The programs needed to create and run a PostgreSQL
                           : server
postgresql15-server.x86_64 : The programs needed to create and run a PostgreSQL
                           : server
postgresql16-server.x86_64 : The programs needed to create and run a PostgreSQL
                           : server

 

postgresql 11, 12, 13, 14, 15, 16 설치가 가능해졌습니다.

 

 

PostgreSQL 14 인스톨

CentOS8에 PostgreSQL 14를 인스톨하겠습니다.

dnf install -y postgresql14-server

[root@test ~]# dnf install -y postgresql14-server
Last metadata expiration check: 0:14:09 ago on Thu 14 Dec 2023 02:34:04 PM JST.
Dependencies resolved.
================================================================================
 Package               Arch     Version               Repository           Size
================================================================================
Installing:
 postgresql14-server   x86_64   14.10-2PGDG.rhel8     pgdg14              5.7 M
Installing dependencies:
 lz4                   x86_64   1.8.3-3.el8_4         ol8_baseos_latest   103 k
 postgresql14          x86_64   14.10-2PGDG.rhel8     pgdg14              1.5 M
 postgresql14-libs     x86_64   14.10-2PGDG.rhel8     pgdg14              280 k

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

Total download size: 7.6 M
Installed size: 32 M
Downloading Packages:
(1/4): lz4-1.8.3-3.el8_4.x86_64.rpm             188 kB/s | 103 kB     00:00
(2/4): postgresql14-libs-14.10-2PGDG.rhel8.x86_ 111 kB/s | 280 kB     00:02
(3/4): postgresql14-14.10-2PGDG.rhel8.x86_64.rp 238 kB/s | 1.5 MB     00:06
(4/4): postgresql14-server-14.10-2PGDG.rhel8.x8 628 kB/s | 5.7 MB     00:09
--------------------------------------------------------------------------------
Total                                           789 kB/s | 7.6 MB     00:09
PostgreSQL 14 for RHEL / Rocky 8 - x86_64       1.6 MB/s | 1.7 kB     00:00
Importing GPG key 0x442DF0F8:
 Userid     : "PostgreSQL RPM Building Project <pgsql-pkg-yum@postgresql.org>"
 Fingerprint: 68C9 E2B9 1A37 D136 FE74 D176 1F16 D2E1 442D F0F8
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1
  Installing       : postgresql14-libs-14.10-2PGDG.rhel8.x86_64             1/4
  Running scriptlet: postgresql14-libs-14.10-2PGDG.rhel8.x86_64             1/4
  Installing       : lz4-1.8.3-3.el8_4.x86_64                               2/4
  Installing       : postgresql14-14.10-2PGDG.rhel8.x86_64                  3/4
  Running scriptlet: postgresql14-14.10-2PGDG.rhel8.x86_64                  3/4
  Running scriptlet: postgresql14-server-14.10-2PGDG.rhel8.x86_64           4/4
  Installing       : postgresql14-server-14.10-2PGDG.rhel8.x86_64           4/4
  Running scriptlet: postgresql14-server-14.10-2PGDG.rhel8.x86_64           4/4
  Verifying        : lz4-1.8.3-3.el8_4.x86_64                               1/4
  Verifying        : postgresql14-14.10-2PGDG.rhel8.x86_64                  2/4
  Verifying        : postgresql14-libs-14.10-2PGDG.rhel8.x86_64             3/4
  Verifying        : postgresql14-server-14.10-2PGDG.rhel8.x86_64           4/4

Installed:
  lz4-1.8.3-3.el8_4.x86_64
  postgresql14-14.10-2PGDG.rhel8.x86_64
  postgresql14-libs-14.10-2PGDG.rhel8.x86_64
  postgresql14-server-14.10-2PGDG.rhel8.x86_64

Complete!

 

설치가 완료되었습니다.

설치가 제대로 되었는지 버전을 확인해보겠습니다.

[root@test ~]# psql --version
psql (PostgreSQL) 14.10

 

PostgreSQL 14.10 버전이 표시되었습니다.

 

PostgreSQL 환경 변수 확인

postgres 유저로 전환해서 PGDATA값을 확인하겠습니다.

PGDATA 기본값은 /var/lib/pgsql/14/data 입니다.

[root@test ~]# su - postgres
[postgres@test ~]$ env | grep PGDATA
PGDATA=/var/lib/pgsql/14/data
[postgres@test ~]$ exit
logout
[root@test ~]#

 

PostgreSQL 데이터 베이스 초기화

PostgreSQL를 기동하기 전에 초기화를 합니다.

[root@test ~]# PGSETUP_INITDB_OPTIONS='--encoding=UTF-8 --no-locale' /usr/pgsql-14/bin/postgresql-14-setup initdb
Initializing database ... OK

 

커맨드를 실행하면 PGDATA값에 설정되어 있는 경로에 데이터베이스 클러스터가 작성됩니다.

파일이 생성되었는지 확인해보겠습니다.

[root@test ~]# ll /var/lib/pgsql/14/data/
total 56
drwx------. 5 postgres postgres    41 Dec 14 16:06 base
drwx------. 2 postgres postgres  4096 Dec 14 16:06 global
drwx------. 2 postgres postgres     6 Dec 14 16:06 log
drwx------. 2 postgres postgres     6 Dec 14 16:06 pg_commit_ts
drwx------. 2 postgres postgres     6 Dec 14 16:06 pg_dynshmem
-rw-------. 1 postgres postgres  4577 Dec 14 16:06 pg_hba.conf
-rw-------. 1 postgres postgres  1636 Dec 14 16:06 pg_ident.conf
drwx------. 4 postgres postgres    68 Dec 14 16:06 pg_logical
drwx------. 4 postgres postgres    36 Dec 14 16:06 pg_multixact
drwx------. 2 postgres postgres     6 Dec 14 16:06 pg_notify
drwx------. 2 postgres postgres     6 Dec 14 16:06 pg_replslot
drwx------. 2 postgres postgres     6 Dec 14 16:06 pg_serial
drwx------. 2 postgres postgres     6 Dec 14 16:06 pg_snapshots
drwx------. 2 postgres postgres     6 Dec 14 16:06 pg_stat
drwx------. 2 postgres postgres     6 Dec 14 16:06 pg_stat_tmp
drwx------. 2 postgres postgres    18 Dec 14 16:06 pg_subtrans
drwx------. 2 postgres postgres     6 Dec 14 16:06 pg_tblspc
drwx------. 2 postgres postgres     6 Dec 14 16:06 pg_twophase
-rw-------. 1 postgres postgres     3 Dec 14 16:06 PG_VERSION
drwx------. 3 postgres postgres    60 Dec 14 16:06 pg_wal
drwx------. 2 postgres postgres    18 Dec 14 16:06 pg_xact
-rw-------. 1 postgres postgres    88 Dec 14 16:06 postgresql.auto.conf
-rw-------. 1 postgres postgres 28724 Dec 14 16:06 postgresql.conf

 

/var/lib/pgsql/14/data/ 경로에 파일이 생성되었습니다.

 

PostgreSQL 기동

설치가 모두 끝났으니 PostgreSQL 시작하겠습니다.

먼저 PostgreSQL 상태를 확인해보겠습니다.

[root@test ~]# systemctl status postgresql-14
● postgresql-14.service - PostgreSQL 14 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-14.service; disabled; ven>
   Active: inactive (dead)
     Docs: https://www.postgresql.org/docs/14/static/

 

현재 PostgreSQL은 멈춰있는 상태입니다.

start 명령어를 사용해 기동을 하겠습니다.

[root@test ~]# systemctl start postgresql-14

 

기동후 다시 상태를 확인해보겠습니다.

[root@test ~]# systemctl status postgresql-14 
 postgresql-14.service - PostgreSQL 14 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-14.service; disabled; ven>
   Active: active (running) since Thu 2023-12-14 16:10:18 JST; 1min 9s ago
     Docs: https://www.postgresql.org/docs/14/static/
  Process: 11899 ExecStartPre=/usr/pgsql-14/bin/postgresql-14-check-db-dir ${PG>
 Main PID: 11905 (postmaster)
    Tasks: 8 (limit: 5788)
   Memory: 22.6M
   CGroup: /system.slice/postgresql-14.service
           tq11905 /usr/pgsql-14/bin/postmaster -D /var/lib/pgsql/14/data/
           tq11906 postgres: logger
           tq11908 postgres: checkpointer
           tq11909 postgres: background writer
           tq11910 postgres: walwriter
           tq11911 postgres: autovacuum launcher
           tq11912 postgres: stats collector
           mq11913 postgres: logical replication launcher

Dec 14 16:10:18 test systemd[1]: Starting PostgreSQL 14 database server...
Dec 14 16:10:18 test postmaster[11905]: 2023-12-14 16:10:18.794 JST [11905] LOG>
Dec 14 16:10:18 test postmaster[11905]: 2023-12-14 16:10:18.794 JST [11905] HIN>
Dec 14 16:10:18 test systemd[1]: Started PostgreSQL 14 database server.

 

PostgreSQL가 기동 되었습니다.

 

PostgreSQL 접속 확인

기동을 했으니 PostgreSQL에 접속이 되는지 확인해보겠습니다.

[root@test 14]# su - postgres
Last login: Thu Dec 14 15:59:34 JST 2023 on pts/0

[postgres@test ~]$ psql
psql (14.10)
Type "help" for help.

postgres=# select version();
                                                 version
----------------------------------------------------------------------------------------------------------
 PostgreSQL 14.10 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-20), 64-bit
(1 row)

postgres=#

 

PostgreSQL에 접속하기 위해 CentOS에서 postgres유저로 전환을 했습니다.

CentOS에서 psql 커맨드로 PostgreSQL 접속합니다.

테스트로 select version(); 실행한 결과 버전이 표시가 되었습니다.

댓글