公開日:2023/8/10 0:00:00

AlmaLinux 9.2 セットアップメモ

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

About

PostgreSQLのインストールと設定手順を残しています。

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

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

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

モジュールの中から使うことができるバージョン情報から確認します。

[root@localhost ~]# dnf module list postgresql
AlmaLinux 9 - AppStream
Name                    Stream              Profiles                    Summary
postgresql              15                  client, server              PostgreSQL server and client module

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

PostgreSQL 15 しか登録されていないことが分かります。

バージョンの切り替え

一つしかないですが、念のためパッケージをインストールする前に、対象バージョンを使うように設定します

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

[root@localhost ~]# dnf module enable postgresql:15
Last metadata expiration check: 0:27:23 ago on Wed Aug  9 09:27:41 2023.
Dependencies resolved.
========================================================================================================================
 Package                     Architecture               Version                       Repository                   Size
========================================================================================================================
Enabling module streams:
 postgresql                                             15

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

Is this ok [y/N]: y
Complete!
[root@localhost ~]# dnf module list postgresql
AlmaLinux 9 - AppStream
Name                    Stream              Profiles                    Summary
postgresql              15 [e]              client, server              PostgreSQL server and client module

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

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

インストール

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

[root@localhost ~]# dnf install postgresql postgresql-server
Dependencies resolved.
========================================================================================================================
 Package                          Architecture    Version                                      Repository          Size
========================================================================================================================
Installing:
 postgresql                       x86_64          15.3-1.module_el9.2.0+32+f3c125b5            appstream          1.6 M
 postgresql-server                x86_64          15.3-1.module_el9.2.0+32+f3c125b5            appstream          6.1 M
Installing dependencies:
 libicu                           x86_64          67.1-9.el9                                   baseos             9.6 M
 postgresql-private-libs          x86_64          15.3-1.module_el9.2.0+32+f3c125b5            appstream          138 k

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

Total download size: 17 M
Installed size: 64 M
Is this ok [y/N]: y
Downloading Packages:
(1/4): postgresql-private-libs-15.3-1.module_el9.2.0+32+f3c125b5.x86_64.rpm             680 kB/s | 138 kB     00:00
(2/4): postgresql-15.3-1.module_el9.2.0+32+f3c125b5.x86_64.rpm                          2.3 MB/s | 1.6 MB     00:00
(3/4): postgresql-server-15.3-1.module_el9.2.0+32+f3c125b5.x86_64.rpm                   3.9 MB/s | 6.1 MB     00:01
(4/4): libicu-67.1-9.el9.x86_64.rpm                                                     4.2 MB/s | 9.6 MB     00:02
------------------------------------------------------------------------------------------------------------------------
Total                                                                                   4.0 MB/s |  17 MB     00:04
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                1/1
  Installing       : postgresql-private-libs-15.3-1.module_el9.2.0+32+f3c125b5.x86_64                               1/4
  Installing       : postgresql-15.3-1.module_el9.2.0+32+f3c125b5.x86_64                                            2/4
  Installing       : libicu-67.1-9.el9.x86_64                                                                       3/4
  Running scriptlet: postgresql-server-15.3-1.module_el9.2.0+32+f3c125b5.x86_64                                     4/4
  Installing       : postgresql-server-15.3-1.module_el9.2.0+32+f3c125b5.x86_64                                     4/4
  Running scriptlet: postgresql-server-15.3-1.module_el9.2.0+32+f3c125b5.x86_64                                     4/4
  Verifying        : postgresql-15.3-1.module_el9.2.0+32+f3c125b5.x86_64                                            1/4
  Verifying        : postgresql-private-libs-15.3-1.module_el9.2.0+32+f3c125b5.x86_64                               2/4
  Verifying        : postgresql-server-15.3-1.module_el9.2.0+32+f3c125b5.x86_64                                     3/4
  Verifying        : libicu-67.1-9.el9.x86_64                                                                       4/4

Installed:
  libicu-67.1-9.el9.x86_64
  postgresql-15.3-1.module_el9.2.0+32+f3c125b5.x86_64
  postgresql-private-libs-15.3-1.module_el9.2.0+32+f3c125b5.x86_64
  postgresql-server-15.3-1.module_el9.2.0+32+f3c125b5.x86_64

Complete!
[root@localhost ~]#

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

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

Databaseに記録された情報がディスク内のどこに保存されるか?その場所は変更できるのか?という事に対する対応方法については、SELinuxが有効になっているため複雑なことになっているのでここでの解説はしません。

SELinux コンテキスト、ラベルを適切に設定したり、それの永続化など考えると長くなってしまうので……

データベースの初期化

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

[root@localhost ~]# /usr/bin/postgresql-setup --initdb --unit postgresql
 * Initializing database in '/var/lib/pgsql/data'
 * Initialized, logs are in /var/lib/pgsql/initdb_postgresql.log
[root@localhost ~]$ 

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

なお、間違った作られたファイルを消したい……となったとしても、/var/lib/pgsql は削除しないでください。 SELinuxコンテキストが変わってしまいサービスが起動できないなどの不具合が出るかもしれないので。

サービス

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

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

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

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

[root@localhost ~]# cp --preserve=context /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 --preserve=context /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
***************
*** 84,94 ****
        # "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
--- 84,94 ----
        # "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 (13.7)
"help"でヘルプを表示します。

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