このセクションでは、データベースサーバーの PostgreSQL のインストールについて紹介します。

CentOS のバージョンを確認します。

[root@db-1 ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core) 
[root@db-1 ~]# 

最新バージョンの PostgreSQL 11をインストールするために、 PostgreSQL の公式リポジトリをインストールします。

[root@db-1 ~]# yum -y localinstall https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7.6-ppc64le/pgdg-centos11-11-2.noarch.rpm
Loaded plugins: fastestmirror
pgdg-centos11-11-2.noarch.rpm                            | 4.8 kB     00:00     
Examining /var/tmp/yum-root-q4rO6o/pgdg-centos11-11-2.noarch.rpm: pgdg-centos11-11-2.noarch
Marking /var/tmp/yum-root-q4rO6o/pgdg-centos11-11-2.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package pgdg-centos11.noarch 0:11-2 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package            Arch        Version   Repository                       Size
================================================================================
Installing:
 pgdg-centos11      noarch      11-2      /pgdg-centos11-11-2.noarch      2.7 k

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

Total size: 2.7 k
Installed size: 2.7 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : pgdg-centos11-11-2.noarch                                    1/1 
  Verifying  : pgdg-centos11-11-2.noarch                                    1/1 

Installed:
  pgdg-centos11.noarch 0:11-2                                                   

Complete!
[root@db-1 ~]# 

yum リポジトリで PostgreSQL 11 のパッケージがあることを確認します。

[root@db-1 ~]# yum info postgresql11-server
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: ftp.riken.jp
pgdg11                                                   | 4.1 kB     00:00     
(1/2): pgdg11/7/x86_64/group_gz                            |  245 B   00:03     
(2/2): pgdg11/7/x86_64/primary_db                          | 141 kB   00:04     
Available Packages
Name        : postgresql11-server
Arch        : x86_64
Version     : 11.1
Release     : 1PGDG.rhel7
Size        : 4.7 M
Repo        : pgdg11/7/x86_64
Summary     : The programs needed to create and run a PostgreSQL server
URL         : https://www.postgresql.org/
License     : PostgreSQL
Description : PostgreSQL is an advanced Object-Relational database management
            : system (DBMS). The postgresql11-server package contains the
            : programs needed to create and run a PostgreSQL server, which will
            : in turn allow you to create and maintain PostgreSQL databases.

[root@db-1 ~]# 

PostgreSQL 11 をインストールします。

[root@db-1 ~]# yum -y install postgresql11-server
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: ftp.riken.jp
Resolving Dependencies
--> Running transaction check
---> Package postgresql11-server.x86_64 0:11.1-1PGDG.rhel7 will be installed
--> Processing Dependency: postgresql11-libs(x86-64) = 11.1-1PGDG.rhel7 for package: postgresql11-server-11.1-1PGDG.rhel7.x86_64
--> Processing Dependency: postgresql11(x86-64) = 11.1-1PGDG.rhel7 for package: postgresql11-server-11.1-1PGDG.rhel7.x86_64
--> Processing Dependency: libpq.so.5()(64bit) for package: postgresql11-server-11.1-1PGDG.rhel7.x86_64
--> Processing Dependency: libicuuc.so.50()(64bit) for package: postgresql11-server-11.1-1PGDG.rhel7.x86_64
--> Processing Dependency: libicui18n.so.50()(64bit) for package: postgresql11-server-11.1-1PGDG.rhel7.x86_64
--> Running transaction check
---> Package libicu.x86_64 0:50.1.2-17.el7 will be installed
---> Package postgresql11.x86_64 0:11.1-1PGDG.rhel7 will be installed
---> Package postgresql11-libs.x86_64 0:11.1-1PGDG.rhel7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                  Arch        Version                 Repository   Size
================================================================================
Installing:
 postgresql11-server      x86_64      11.1-1PGDG.rhel7        pgdg11      4.7 M
Installing for dependencies:
 libicu                   x86_64      50.1.2-17.el7           base        6.9 M
 postgresql11             x86_64      11.1-1PGDG.rhel7        pgdg11      1.6 M
 postgresql11-libs        x86_64      11.1-1PGDG.rhel7        pgdg11      359 k

Transaction Summary
================================================================================
Install  1 Package (+3 Dependent packages)

Total download size: 14 M
Installed size: 53 M
Downloading packages:
(1/4): libicu-50.1.2-17.el7.x86_64.rpm                     | 6.9 MB   00:04     
(2/4): postgresql11-libs-11.1-1PGDG.rhel7.x86_64.rpm       | 359 kB   00:05     
(3/4): postgresql11-11.1-1PGDG.rhel7.x86_64.rpm            | 1.6 MB   00:08     
(4/4): postgresql11-server-11.1-1PGDG.rhel7.x86_64.rpm     | 4.7 MB   00:03     
--------------------------------------------------------------------------------
Total                                              1.4 MB/s |  14 MB  00:09     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : libicu-50.1.2-17.el7.x86_64                                  1/4 
  Installing : postgresql11-libs-11.1-1PGDG.rhel7.x86_64                    2/4 
  Installing : postgresql11-11.1-1PGDG.rhel7.x86_64                         3/4 
  Installing : postgresql11-server-11.1-1PGDG.rhel7.x86_64                  4/4 
  Verifying  : postgresql11-libs-11.1-1PGDG.rhel7.x86_64                    1/4 
  Verifying  : postgresql11-11.1-1PGDG.rhel7.x86_64                         2/4 
  Verifying  : libicu-50.1.2-17.el7.x86_64                                  3/4 
  Verifying  : postgresql11-server-11.1-1PGDG.rhel7.x86_64                  4/4 

Installed:
  postgresql11-server.x86_64 0:11.1-1PGDG.rhel7                                 

Dependency Installed:
  libicu.x86_64 0:50.1.2-17.el7                                                 
  postgresql11.x86_64 0:11.1-1PGDG.rhel7                                        
  postgresql11-libs.x86_64 0:11.1-1PGDG.rhel7                                   

Complete!
[root@db-1 ~]# 

インストールした PostgreSQL のバージョンを確認します。バージョン 11 であることが確認できます。

[root@db-1 ~]# /usr/pgsql-11/bin/postgres --version
postgres (PostgreSQL) 11.1
[root@db-1 ~]# 

データベースの初期化を行います。

[root@db-1 ~]# /usr/pgsql-11/bin/postgresql-11-setup initdb
Initializing database ... OK

[root@db-1 ~]# 

PostgreSQL は主なファイルは以下にインストールされています。

/var/lib/pgsql/11/data/postgresql.conf PostgreSQL のメイン設定ファイル
/var/lib/pgsql/11/data/pg_hba.conf PostgreSQL のユーザ認証に関わる設定ファイル
/usr/pgsql-11/bin/PostgreSQL の実行コマンドが格納されていディレクトリ

PostgreSQL サービスの自動起動を有効化します。

■ 自動起動の有効化
[root@db-1 ~]# systemctl enable postgresql-11
Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-11.service to /usr/lib/systemd/system/postgresql-11.service.
[root@db-1 ~]# 

■ 自動起動の状態確認
[root@db-1 ~]# systemctl is-enabled postgresql-11
enabled
[root@db-1 ~]# 

PostgreSQL サービスを起動します。

■ 起動
[root@db-1 ~]# systemctl start postgresql-11
[root@db-1 ~]# 

■ 起動状態の確認
[root@db-1 ~]# systemctl status postgresql-11
● postgresql-11.service - PostgreSQL 11 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-11.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2018-12-31 15:21:10 JST; 6s ago
     Docs: https://www.postgresql.org/docs/11/static/
  Process: 23121 ExecStartPre=/usr/pgsql-11/bin/postgresql-11-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 23127 (postmaster)
   CGroup: /system.slice/postgresql-11.service
           ├─23127 /usr/pgsql-11/bin/postmaster -D /var/lib/pgsql/11/data/
           ├─23128 postgres: logger   
           ├─23130 postgres: checkpointer   
           ├─23131 postgres: background writer   
           ├─23132 postgres: walwriter   
           ├─23133 postgres: autovacuum launcher   
           ├─23134 postgres: stats collector   
           └─23135 postgres: logical replication launcher   

Dec 31 15:21:09 db-1.novalocal systemd[1]: Starting PostgreSQL 11 database s....
Dec 31 15:21:09 db-1.novalocal postmaster[23127]: 2018-12-31 15:21:09.953 JST...
Dec 31 15:21:09 db-1.novalocal postmaster[23127]: 2018-12-31 15:21:09.955 JST...
Dec 31 15:21:09 db-1.novalocal postmaster[23127]: 2018-12-31 15:21:09.988 JST...
Dec 31 15:21:10 db-1.novalocal postmaster[23127]: 2018-12-31 15:21:10.013 JST...
Dec 31 15:21:10 db-1.novalocal postmaster[23127]: 2018-12-31 15:21:10.102 JST...
Dec 31 15:21:10 db-1.novalocal postmaster[23127]: 2018-12-31 15:21:10.102 JST...
Dec 31 15:21:10 db-1.novalocal systemd[1]: Started PostgreSQL 11 database se....
Hint: Some lines were ellipsized, use -l to show in full.
[root@db-1 ~]# 

データベースへの接続を確認します。

[root@db-1 ~]# sudo -u postgres psql -U postgres
could not change directory to "/root": Permission denied
psql (11.1)
Type "help" for help.

postgres=# \q
[root@db-1 ~]#