RHEL/OracleLinux/CentOSにPostgreSQLをインストールする手順

RHEL/OracleLinux/CentOSにPostgreSQLをインストールする手順

Redhat系ディストリビューションにPostgreSQLをインストールする手順になります。

  • RHEL
  • OracleLinux
  • CentOS

本手順はOracleLinux8で手順を実施します。

Ubuntuのインストールは以下に用意しておりますので、こちらも合わせて参考にしてみてください。

https://shimi-dai.com/postgresql-install-on-ubuntu/

リポジトリの取得とデフォルトで入っているPostgreSQLの無効化

最新のPostgreSQLリポジトリを取得します。

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

インストールログ ____

Extra Packages for Enterprise Linux Modular 8 - x86_64                                                          1.3 MB/s | 979 kB     00:00
Extra Packages for Enterprise Linux 8 - x86_64                                                                  5.5 MB/s |  11 MB     00:01
nginx repo                                                                                                       51 kB/s |  92 kB     00:01
Oracle Linux 8 BaseOS Latest (x86_64)                                                                            11 MB/s |  41 MB     00:03
Oracle Linux 8 Application Stream (x86_64)                                                                       10 MB/s |  31 MB     00:03
Latest Unbreakable Enterprise Kernel Release 6 for Oracle Linux 8 (x86_64)                                       10 MB/s |  32 MB     00:03
Zabbix Official Repository - x86_64                                                                             278 kB/s | 218 kB     00:00
Zabbix Official Repository non-supported - x86_64                                                               2.6 kB/s | 1.2 kB     00:00
pgdg-redhat-repo-latest.noarch.rpm                                                                               10 kB/s |  12 kB     00:01
依存関係が解決しました。
================================================================================================================================================
 パッケージ                              アーキテクチャー              バージョン                     リポジトリー                        サイズ
================================================================================================================================================
インストール:
 pgdg-redhat-repo                        noarch                        42.0-23                        @commandline                         12 k

トランザクションの概要
================================================================================================================================================
インストール  1 パッケージ

合計サイズ: 12 k
インストール後のサイズ: 12 k
パッケージのダウンロード:
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  準備             :                                                                                                                        1/1
  インストール中   : pgdg-redhat-repo-42.0-23.noarch                                                                                        1/1
  検証             : pgdg-redhat-repo-42.0-23.noarch                                                                                        1/1

インストール済み:
  pgdg-redhat-repo-42.0-23.noarch

完了しました!

現在入っているPostgreSQLは無効化します。

dnf -qy module disable postgresql

インストールログ ____

GPG 鍵 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
GPG 鍵 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
GPG 鍵 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
GPG 鍵 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
GPG 鍵 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
GPG 鍵 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
GPG 鍵 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を実際にインストールするコマンドは以下になります。

dnf install -y postgresql14-server

インストールログ ____

メタデータの期限切れの最終確認: 0:03:35 時間前の 2022年01月12日 01時52分26秒 に実施しました。
依存関係が解決しました。
================================================================================================================================================
 パッケージ                               アーキテクチャー            バージョン                              リポジトリー                サイズ
================================================================================================================================================
インストール:
 postgresql14-server                      x86_64                      14.1-1PGDG.rhel8                        pgdg14                      5.7 M
依存関係のインストール:
 postgresql14                             x86_64                      14.1-1PGDG.rhel8                        pgdg14                      1.5 M
 postgresql14-libs                        x86_64                      14.1-1PGDG.rhel8                        pgdg14                      274 k

トランザクションの概要
================================================================================================================================================
インストール  3 パッケージ

ダウンロードサイズの合計: 7.5 M
インストール後のサイズ: 31 M
パッケージのダウンロード:
(1/3): postgresql14-libs-14.1-1PGDG.rhel8.x86_64.rpm                                                            124 kB/s | 274 kB     00:02
(2/3): postgresql14-14.1-1PGDG.rhel8.x86_64.rpm                                                                 368 kB/s | 1.5 MB     00:04
(3/3): postgresql14-server-14.1-1PGDG.rhel8.x86_64.rpm                                                          601 kB/s | 5.7 MB     00:09
------------------------------------------------------------------------------------------------------------------------------------------------
合計                                                                                                            788 kB/s | 7.5 MB     00:09
警告: /var/cache/dnf/pgdg14-28b8f0a4326d2bf5/packages/postgresql14-14.1-1PGDG.rhel8.x86_64.rpm: ヘッダー V4 DSA/SHA1 Signature、鍵 ID 442df0f8: NOKEY
PostgreSQL 14 for RHEL/CentOS 8 - x86_64                                                                        1.6 MB/s | 1.7 kB     00:00
GPG 鍵 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
鍵のインポートに成功しました
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  準備             :                                                                                                                        1/1
  インストール中   : postgresql14-libs-14.1-1PGDG.rhel8.x86_64                                                                              1/3
  scriptletの実行中: postgresql14-libs-14.1-1PGDG.rhel8.x86_64                                                                              1/3
/sbin/ldconfig: /etc/ld.so.conf.d/kernel-5.4.17-2011.7.4.el8uek.x86_64.conf:6: hwcap directive ignored

  インストール中   : postgresql14-14.1-1PGDG.rhel8.x86_64                                                                                   2/3
  scriptletの実行中: postgresql14-14.1-1PGDG.rhel8.x86_64                                                                                   2/3
  scriptletの実行中: postgresql14-server-14.1-1PGDG.rhel8.x86_64                                                                            3/3
  インストール中   : postgresql14-server-14.1-1PGDG.rhel8.x86_64                                                                            3/3
  scriptletの実行中: postgresql14-server-14.1-1PGDG.rhel8.x86_64                                                                            3/3
/sbin/ldconfig: /etc/ld.so.conf.d/kernel-5.4.17-2011.7.4.el8uek.x86_64.conf:6: hwcap directive ignored

/sbin/ldconfig: /etc/ld.so.conf.d/kernel-5.4.17-2011.7.4.el8uek.x86_64.conf:6: hwcap directive ignored

  検証             : postgresql14-14.1-1PGDG.rhel8.x86_64                                                                                   1/3
  検証             : postgresql14-libs-14.1-1PGDG.rhel8.x86_64                                                                              2/3
  検証             : postgresql14-server-14.1-1PGDG.rhel8.x86_64                                                                            3/3

インストール済み:
  postgresql14-14.1-1PGDG.rhel8.x86_64       postgresql14-libs-14.1-1PGDG.rhel8.x86_64       postgresql14-server-14.1-1PGDG.rhel8.x86_64

完了しました!

PostgreSQL14ではpostgresユーザ(postgres用のOSユーザ)が存在しない場合、自動で作成されます。

# id postgres
uid=26(postgres) gid=26(postgres) groups=26(postgres)

データベースの初期化

ソフトウェアをインストールしたらDB初期化をします。rootユーザで実行してください。

/usr/pgsql-14/bin/postgresql-14-setup initdb

以下のようになればOKです。

# /usr/pgsql-14/bin/postgresql-14-setup initdb
Initializing database ... OK

データベースの自動起動の有効化と起動

systemcltに自動起動設定を追加します。

systemctl enable --now postgresql-14


# systemctl enable --now postgresql-14
Created symlink /etc/systemd/system/multi-user.target.wants/postgresql-14.service → /usr/lib/systemd/system/postgresql-14.service.

起動方法は以下になります。

systemctl start postgresql-14

起動したPostgreSQLの状態は以下のようになります。

# systemctl status postgresql-14
● postgresql-14.service - PostgreSQL 14 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-14.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2022-01-11 11:30:54 JST; 13s ago
     Docs: https://www.postgresql.org/docs/14/static/
  Process: 17867 ExecStartPre=/usr/pgsql-14/bin/postgresql-14-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 17884 (postmaster)
    Tasks: 8 (limit: 11421)
   Memory: 16.8M
   CGroup: /system.slice/postgresql-14.service
           ├─17884 /usr/pgsql-14/bin/postmaster -D /var/lib/pgsql/14/data/
           ├─17919 postgres: logger
           ├─17937 postgres: checkpointer
           ├─17938 postgres: background writer
           ├─17939 postgres: walwriter
           ├─17940 postgres: autovacuum launcher
           ├─17941 postgres: stats collector
           └─17942 postgres: logical replication launcher

 1月 11 11:30:54 118-27-6-45 systemd[1]: Starting PostgreSQL 14 database server...
 1月 11 11:30:54 118-27-6-45 postmaster[17884]: 2022-01-11 11:30:54.496 JST [17884] LOG:  redirecting log output to logging collector process
 1月 11 11:30:54 118-27-6-45 postmaster[17884]: 2022-01-11 11:30:54.496 JST [17884] HINT:  Future log output will appear in directory "log".
 1月 11 11:30:54 118-27-6-45 systemd[1]: Started PostgreSQL 14 database server.
技術ブログ一覧へ戻る