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

AlmaLinux 8.10

AlmaLinux 8.10 セットアップメモ

Section 3.1 データベースサーバー構築編(PostgreSQL)

About

MySQLやmariadbに比べて、引っかかることが多いので、PostgreSQLのインストールと設定手順を残しています。

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

なお、この手順では、OS側で管理しているパッケージを使った構築手順となります。いろいろ付随するものを入れる場合、dnf管理されていないこともあるので、拡張機能を入れる場合は、Section3.2のPostgreSQL公式パッケージからのインストール手順を使ってください。

AppStreamのバージョン等を調べる

RHEL 8 から、OSのライフサイクル(リリースから10年)にすべて縛られると更新が早いモノは、大変面倒なことになるので2~5年程度で更新されるようにするAppStreamというものができました。

PostgreSQLも、おおよそ1年に一回アップデートされていくので、RHEL 7の頃は公式リポジトリから持ってきてインストールするのが常でした。今回はその呪縛から逃れられそうなので、dnfを用いてAppStreamから入れていきたいと思います。

まずは、使うことができるバージョン情報から確認します。

[root@localhost ~]# dnf module list postgresql
AlmaLinux 8 - AppStream
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
postgresql             16                 client, server [d]             PostgreSQL server and client module

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

PostgreSQL 10 がデフォルトらしく、16まであるという事がわかりました。

バージョンの切り替え

デフォルト以外のバージョンが必要な場合は、パッケージをインストールする前に、対象バージョンを使うように設定する必要があります。

今回は最新に合わせようという事で、16 を選択します。

[root@localhost ~]# dnf -y module enable postgresql:16
AlmaLinux 8 - AppStream
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
postgresql             16 [e]             client, server [d]             PostgreSQL server and client module

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

これで、16 に [e] が付いたので有効化されました。

インストール

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

[root@localhost ~]# dnf -y install postgresql postgresql-server
依存関係が解決しました。
========================================================================================================================
 パッケージ                      Arch           バージョン                                      リポジトリー      サイズ
========================================================================================================================
インストール:
 postgresql                      x86_64         16.8-1.module_el8.10.0+3969+2c3c9353            appstream         1.9 M
 postgresql-server               x86_64         16.8-1.module_el8.10.0+3969+2c3c9353            appstream         6.9 M
依存関係のインストール:
 libicu                          x86_64         60.3-2.el8_1                                    baseos            8.8 M
 postgresql-private-libs         x86_64         16.8-1.module_el8.10.0+3969+2c3c9353            appstream         135 k

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

(中略)

インストール済み:
  libicu-60.3-2.el8_1.x86_64
  postgresql-16.8-1.module_el8.10.0+3969+2c3c9353.x86_64
  postgresql-private-libs-16.8-1.module_el8.10.0+3969+2c3c9353.x86_64
  postgresql-server-16.8-1.module_el8.10.0+3969+2c3c9353.x86_64

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

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

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

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

特に変更しない場合、データは、/var/lib/pgsql/data/ に保存されます。

変更する場合は、systemd のserviceファイルを書き換えます。

[root@localhost ~]# vi /usr/lib/syst//system/postgresql.service
Environment=PG_OOM_ADJUST_VALUE=0

Environment=PGDATA=/var/lib/pgsql/data

ExecStartPre=/usr/libexec/postgresql-check-db-dir %N

PGDATA が保存先になっているので、保存先を作成した後に書き換えてください。

データベースの初期化

DBの実体を保存する場所や設定なども無いので、それらもまとめて初期化します。

[root@localhost ~]# /usr/bin/postgresql-setup initdb
WARNING: using obsoleted argument syntax, try --help
WARNING: arguments transformed to: postgresql-setup --initdb --unit postgresql
 * Initializing database in '/var/lib/pgsql/data'
 * Initialized, logs are in /var/lib/pgsql/initdb_postgresql.log
[root@localhost ~]#

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

サービス

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

[root@localhost ~]# systemctl enable postgresql
Created symlink /etc/systemd/system/multi-user.target.wants/postgresql.service → /usr/lib/syst//system/postgresql.service.
[root@localhost ~]# systemctl start postgresql
[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
***************
*** 83,93 ****
        # "local" is for Unix domain socket connections only
        local   all             all                                     peer
        # IPv4 local connections:
!       host    all             all             127.0.0.1/32            ident
        # IPv6 local connections:
!       host    all             all             ::1/128                 ident
        # Allow replication connections from localhost, by a user with the
        # replication privilege.
        local   replication     all                                     peer
!       host    replication     all             127.0.0.1/32            ident
!       host    replication     all             ::1/128                 ident
--- 83,93 ----
        # "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
!       host    replication     all             127.0.0.1/32            scram-sha-256
!       host    replication     all             ::1/128                 scram-sha-256

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

[root@localhost ~]# systemctl restart postgresql
[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 (16.8)
"help"でヘルプを表示します。

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