公開日:2025/8/5 0:00:00

AlmaLinux 8.10

AlmaLinux 8.10 セットアップメモ

Section 3.2 データベースサーバー構築編(PostgreSQL 公式パッケージ利用)

About

Section 3.1は、OSの方で管理しているパッケージからインストールしましたが、今回は、PostgreSQLの公式パッケージからインストールしてみます。

環境は、Section 2.1Section 2.3で作成したvirtualboxの環境で実行しています。

AppStreamの設定を変更する

何もしないとOS管理のパッケージをインストールしようとしてしまうため、AppStreamの設定を変えて使わないようにしてしまいます。

[root@localhost ~]# dnf -y module disable postgresql
依存関係が解決しました。
========================================================================================================================
 パッケージ                  アーキテクチャー           バージョン                    リポジトリー                サイズ
========================================================================================================================
モジュールの無効化:
 postgresql

トランザクションの概要
========================================================================================================================

完了しました!
[root@localhost ~]#

状態を確認します。

[root@localhost ~]# dnf module list postgresql
AlmaLinux 8 - AppStream
Name                   Stream               Profiles                      Summary
postgresql             9.6 [x]              client, server [d]            PostgreSQL server and client module
postgresql             10 [d][x]            client, server [d]            PostgreSQL server and client module
postgresql             12 [x]               client, server [d]            PostgreSQL server and client module
postgresql             13 [x]               client, server [d]            PostgreSQL server and client module
postgresql             15 [x]               client, server [d]            PostgreSQL server and client module
postgresql             16 [x]               client, server [d]            PostgreSQL server and client module

ヒント: [d]efault, [e]nabled, [x]disabled, [i]nstalled
[root@localhost ~]#

すべてにxが付いたのでこれで無効化されました。

PostgreSQL 公式サイトのリポジトリを追加

公式サイト(https://www.postgresql.org/) から、Downloads、Linux、Redhat/Rocky/CentOS……とクリックしていけばインストール手順にたどり着けます。

そこに書かれている手順通りに進めていきます。

まずは、リポジトリの追加から。

[root@localhost ~]# dnf -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
依存関係が解決しました。
========================================================================================================================
 パッケージ                       アーキテクチャー       バージョン                  リポジトリー                 サイズ
========================================================================================================================
インストール:
 pgdg-redhat-repo                 noarch                 42.0-54PGDG                 @commandline                  16 k

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

(中略)

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

完了しました!
[root@localhost ~]#

これでリポジトリが追加されました。dnfの仕組みを使ってPostgreSQL公式がパッケージングしているrpmを取得できるようになりました。

バージョンなどの確認

とりあえず検索してみましょう。リポジトリの鍵を有効化するのにいちいち返答するのも手間なので、-y オプションを付けてすべて受けれいてしまいます。

[root@localhost ~]# dnf -y search postgres
PostgreSQL common RPMs for RHEL / Rocky 8 - x86_64                                       87  B/s | 195  B     00:02
PostgreSQL common RPMs for RHEL / Rocky 8 - x86_64                                      1.6 MB/s | 1.7 kB     00:00
(中略)

途中で、いろいろなPostgreSQLのバージョンのカギを有効に……となっているのがわかるとおもいます。変なバージョンを間違って入れてしまうのも問題なのでリポジトリを無効化します。

[root@localhost ~]# dnf repolist --all
repo id                          repo の名前                                                                      状態
(中略)

多すぎるので絞ります。

repo id                          repo の名前                                                                      状態
pgdg-centos8-sysupdates          PostgreSQL Supplementary ucommon RPMs for RHEL / Rocky / AlmaLinux 8 - x86_64    無効化
pgdg-common                      PostgreSQL common RPMs for RHEL / Rocky / AlmaLinux 8 - x86_64                   有効化
pgdg-common-debuginfo            PostgreSQL common RPMs for RHEL / Rocky / AlmaLinux 8 - x86_64 - Debuginfo       無効化
pgdg-common-source               PostgreSQL common for RHEL / Rocky / AlmaLinux 8 - x86_64 - Source               無効化
pgdg-common-testing              PostgreSQL common testing RPMs for RHEL / Rocky / AlmaLinux 8 - x86_64           無効化
pgdg-common-testing-source       PostgreSQL common testing SRPMs for RHEL / Rocky / AlmaLinux 8 - x86_64          無効化
pgdg-rhel8-extras                Extra packages to support some RPMs in the PostgreSQL RPM repo RHEL / Rocky / Al 無効化
pgdg-rhel8-extras-source         SRPMs of the Extras packages to support some RPMs in the PostgreSQL RPM repo RHE 無効化
pgdg-rhel8-extras-testing-source SRPMs of the Extras packages to support some RPMs in the PostgreSQL RPM repo RHE 無効化
pgdg13                           PostgreSQL 13 for RHEL / Rocky / AlmaLinux 8 - x86_64                            有効化
pgdg13-debuginfo                 PostgreSQL 13 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Debuginfo                無効化
pgdg13-source                    PostgreSQL 13 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Source                   無効化
pgdg13-updates-testing           PostgreSQL 13 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Updates testing          無効化
pgdg13-updates-testing-debuginfo PostgreSQL 13 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Debuginfo                無効化
pgdg13-updates-testing-source    PostgreSQL 13 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Source updates testing   無効化
pgdg14                           PostgreSQL 14 for RHEL / Rocky / AlmaLinux 8 - x86_64                            有効化
pgdg14-debuginfo                 PostgreSQL 14 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Debuginfo                無効化
pgdg14-source                    PostgreSQL 14 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Source                   無効化
pgdg14-updates-testing           PostgreSQL 14 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Updates testing          無効化
pgdg14-updates-testing-debuginfo PostgreSQL 14 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Debuginfo                無効化
pgdg14-updates-testing-source    PostgreSQL 14 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Source updates testing   無効化
pgdg15                           PostgreSQL 15 for RHEL / Rocky / AlmaLinux 8 - x86_64                            有効化
pgdg15-debuginfo                 PostgreSQL 15 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Debuginfo                無効化
pgdg15-source                    PostgreSQL 15 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Source                   無効化
pgdg15-updates-testing           PostgreSQL 15 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Updates testing          無効化
pgdg15-updates-testing-debuginfo PostgreSQL 15 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Debuginfo                無効化
pgdg15-updates-testing-source    PostgreSQL 15 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Source updates testing   無効化
pgdg16                           PostgreSQL 16 for RHEL / Rocky / AlmaLinux 8 - x86_64                            有効化
pgdg16-debuginfo                 PostgreSQL 16 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Debuginfo                無効化
pgdg16-source                    PostgreSQL 16 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Source                   無効化
pgdg16-updates-testing           PostgreSQL 16 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Updates testing          無効化
pgdg16-updates-testing-debuginfo PostgreSQL 16 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Debuginfo                無効化
pgdg16-updates-testing-source    PostgreSQL 16 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Source updates testing   無効化
pgdg17                           PostgreSQL 17 for RHEL / Rocky / AlmaLinux 8 - x86_64                            有効化
pgdg17-debuginfo                 PostgreSQL 17 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Debuginfo                無効化
pgdg17-source                    PostgreSQL 17 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Source                   無効化
pgdg17-updates-testing           PostgreSQL 17 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Updates testing          無効化
pgdg17-updates-testing-debuginfo PostgreSQL 17 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Debuginfo                無効化
pgdg17-updates-testing-source    PostgreSQL 17 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Source updates testing   無効化
pgdg18-updates-testing           PostgreSQL 18 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Updates testing          無効化
pgdg18-updates-testing-debuginfo PostgreSQL 18 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Debuginfo                無効化
pgdg18-updates-testing-source    PostgreSQL 18 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Source updates testing   無効化
pgdg19-updates-testing           PostgreSQL 19 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Updates testing          無効化
pgdg19-updates-testing-debuginfo PostgreSQL 19 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Debuginfo                無効化
pgdg19-updates-testing-source    PostgreSQL 19 for RHEL / Rocky / AlmaLinux 8 - x86_64 - Source updates testing   無効化
[root@localhost ~]#

今回使いたい pgdg17 以外を無効化します。

[root@localhost ~]# dnf config-manager --disable pgdg13 pgdg14 pgdg15 pgdg16
[root@localhost ~]# dnf repolist pgdg*
(中略)
[root@localhost ~]# dnf repolist
repo id                            repo の名前
appstream                          AlmaLinux 8 - AppStream
baseos                             AlmaLinux 8 - BaseOS
epel                               Extra Packages for Enterprise Linux 8 - x86_64
extras                             AlmaLinux 8 - Extras
pgdg-common                        PostgreSQL common RPMs for RHEL / Rocky / AlmaLinux 8 - x86_64
pgdg17                             PostgreSQL 17 for RHEL / Rocky / AlmaLinux 8 - x86_64
[root@localhost ~]#

有効だったところが無効になっていると思います。改めてsearchするといろいろ減っている事でしょう。

インストール

対象バージョンが有効になったので普通に指定してインストールしていきましょう。

[root@localhost ~]# dnf -y install postgresql17 postgresql17-server
PostgreSQL common RPMs for RHEL / Rocky / AlmaLinux 8 - x86_64                          3.8 kB/s | 659  B     00:00
PostgreSQL 17 for RHEL / Rocky / AlmaLinux 8 - x86_64                                   3.9 kB/s | 659  B     00:00
依存関係が解決しました。
========================================================================================================================
 パッケージ                         アーキテクチャー      バージョン                        リポジトリー          サイズ
========================================================================================================================
インストール:
 postgresql17                       x86_64                17.5-3PGDG.rhel8                  pgdg17                2.1 M
 postgresql17-server                x86_64                17.5-3PGDG.rhel8                  pgdg17                7.0 M
依存関係のインストール:
 libicu                             x86_64                60.3-2.el8_1                      baseos                8.8 M
 postgresql17-libs                  x86_64                17.5-3PGDG.rhel8                  pgdg17                336 k

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

(中略)

インストール済み:
  libicu-60.3-2.el8_1.x86_64                                postgresql17-17.5-3PGDG.rhel8.x86_64
  postgresql17-libs-17.5-3PGDG.rhel8.x86_64                 postgresql17-server-17.5-3PGDG.rhel8.x86_64

完了しました!
[root@localhost ~]#

インストールが終わりましたが、まだ使えません。

データの保存場所について

Databaseに記録された情報がディスク内のどこに保存されるか?その場所は変更できるのか?という事に対する対応方法。

特に変更しない場合、データは、/var/lib/pgsql/17/data/ に保存されます。OS公式で入れた場合とは異なります。 それでは不便なこともあるので、OS標準のRPMで入れた場合と同じ場所に保存されるように設定を変更します。

systemd のserviceファイル内の PGDATA を書き換えます。

[root@localhost ~]# cp /usr/lib/systemd/system/postgresql-17.service /usr/lib/syst//system/~postgresql-17.service.org
[root@localhost ~]# cat /usr/lib/syst//system/~postgresql-17.service.org \
 | sed -E "s/^(Environment=PGDATA=)(.*)$/\\1\/var\/lib\/pgsql\/data\//" \
 > /usr/lib/syst//system/postgresql-17.service
[root@localhost ~]# diff -cT /usr/lib/systemd/system/~postgresql-17.service.org /usr/lib/syst//system/postgresql-17.service
*** /usr/lib/syst//system/~postgresql-17.service.org  YYYY-mm-dd hh:ii:ss.000000000 +0900
--- /usr/lib/syst//system/postgresql-17.service       YYYY-mm-dd hh:ii:ss.000000000 +0900
***************
*** 26,32 ****
        # break postgresql-setup.

        # Location of database directory
!       Environment=PGDATA=/var/lib/pgsql/17/data/

        # Where to send early-startup messages from the server (before the logging
        # options of postgresql.conf take effect)
--- 26,32 ----
        # break postgresql-setup.

        # Location of database directory
!       Environment=PGDATA=/var/lib/pgsql/data/

        # Where to send early-startup messages from the server (before the logging
        # options of postgresql.conf take effect)

データベースの初期化

公式の手順通りに初期化します。

[root@localhost ~]# /usr/pgsql-17/bin/postgresql-17-setup initdb
Initializing database ... OK

[root@localhost ~]#

これで初期化できたので、サービスを起動していきます。

サービス

systemd を通じてサービスの有効化を行い、起動します。

[root@localhost ~]# systemctl enable postgresql-17
Created symlink /etc/systemd/system/multi-user.target.wants/postgresql-17.service → /usr/lib/syst//system/postgresql-17.service.
[root@localhost ~]# systemctl start postgresql-17
[root@localhost ~]# 

別のサーバーからアクセスさせる場合の設定

初期設定では、外部からのアクセスが完全に禁止されています。 そのため許可するように設定を変更してみましょう。

[root@localhost ~]# cp /var/lib/pgsql/data/postgresql.conf /var/lib/pgsql/data/postgresql.conf.org
[root@localhost ~]# cat /var/lib/pgsql/data/postgresql.conf.org \
| sed -E "/^#listen_addresses .*$/i listen_addresses = '*'" \
> /var/lib/pgsql/data/postgresql.conf
[root@localhost ~]# diff -cT /var/lib/pgsql/data/postgresql.conf.org /var/lib/pgsql/data/postgresql.conf
*** /var/lib/pgsql/data/postgresql.conf.org YYYY-mm-dd hh:ii:ss.000000000 +0900
--- /var/lib/pgsql/data/postgresql.conf     YYYY-mm-dd hh:ii:ss.000000000 +0900
***************
*** 57,62 ****
--- 57,63 ----

        # - Connection Settings -

+       listen_addresses = '*'
        #listen_addresses = 'localhost'         # what IP address(es) to listenon;
                                                # comma-separated list of addresses;
                                                # defaults to 'localhost'; use '*' for all

listen_adresses を書き換えることになりますが、* はすべてを許可になるため、接続元がわかっている場合は制限を掛けるなどしても良いでしょう。

次に接続するユーザーの認証方式などを変更します。local接続がpeerだと不便だったりあまりよくないこともあるのですがとりあえず……

[root@localhost ~]# cp /var/lib/pgsql/data/pg_hba.conf /var/lib/pgsql/data/pg_hba.conf.org
[root@localhost ~]# cat /var/lib/pgsql/data/pg_hba.conf.org \
| sed -E "s/(^host[ ]+all[ ]+all[ ]+)127.0.0.1\/32/\\1all         /" \
| sed -E "s/(^host[ ]+all[ ]+all[ ]+)::1\/128/\\1all    /" \
| sed -E "s/ident$/scram-sha-256/" \
> /var/lib/pgsql/data/pg_hba.conf
[root@localhost ~]# diff -cT /var/lib/pgsql/data/pg_hba.conf.org /var/lib/pgsql/data/pg_hba.conf
*** /var/lib/pgsql/data/pg_hba.conf.org YYYY-mm-dd hh:ii:ss.000000000 +0900
--- /var/lib/pgsql/data/pg_hba.conf     YYYY-mm-dd hh:ii:ss.000000000 +0900
***************
*** 112,120 ****
        # "local" is for Unix domain socket connections only
        local   all             all                                     peer
        # IPv4 local connections:
!       host    all             all             127.0.0.1/32            scram-sha-256
        # IPv6 local connections:
!       host    all             all             ::1/128                 scram-sha-256
        # Allow replication connections from localhost, by a user with the
        # replication privilege.
        local   replication     all                                     peer
--- 112,120 ----
        # "local" is for Unix domain socket connections only
        local   all             all                                     peer
        # IPv4 local connections:
!       host    all             all             all                     scram-sha-256
        # IPv6 local connections:
!       host    all             all             all                     scram-sha-256
        # Allow replication connections from localhost, by a user with the
        # replication privilege.
        local   replication     all                                     peer

設定を変えたらサービスを再起動しましょう。

[root@localhost ~]# systemctl restart postgresql-17
[root@localhost ~]# 

ロールとデータベースを作成する

データベースにアクセスするためのユーザーと権限(ロール)とデータベースを作成します。

vagrant ユーザーがいるのでそのユーザー用のロールとを作成します。オプションは下記の通り。

オプション内容
-Sposgtresロールと同じ権限を持つスーパーユーザーではない
-Rロールは別のロールを作成不可
-dロールは新しいデータベースを作成可
-P新しいロールにパスワードを割り当てる

データベースも作れないようにするのが正解ではあるけれど、何かと面倒なので作成可能にしています。

[root@localhost ~]# su - postgres -c "createuser -S -R -d -P vagrant"
新しいロールのためのパスワード:
もう一度入力してください:
[root@localhost ~]# 

データベースを作成します。

[root@localhost ~]# su - postgres -c "createdb -O vagrant -E UTF-8 vagrant"
[root@localhost ~]# 

接続確認

psqlコマンドを使って接続確認します。

[vagrant@localhost ~]$ psql
psql (13.9)
"help"でヘルプを表示します。

vagrant=> \q
[vagrant@localhost ~]$