公開日:2022/8/22 0:00:00

AlmaLinux 8.6

AlmaLinux 8.6 セットアップメモ

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

About

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

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

AppStreamの設定を変更する

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

[root@localhost ~]# dnf module disable postgresql
========================================================================================================================
 パッケージ                  アーキテクチャー           バージョン                    リポジトリー                サイズ
========================================================================================================================
モジュールの無効化:
 postgresql

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

これでよろしいですか? [y/N]: y
完了しました!
[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

Extra Packages for Enterprise Linux Modular 8 - x86_64
Name                   Stream               Profiles                      Summary
postgresql             11 [x]               client, server                PostgreSQL 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 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-24                  @commandline                   13 k

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

合計サイズ: 13 k
インストール後のサイズ: 12 k
これでよろしいですか? [y/N]: y
パッケージのダウンロード:
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  準備             :                                                                                                1/1
  インストール中   : pgdg-redhat-repo-42.0-24.noarch                                                                1/1
  検証             : pgdg-redhat-repo-42.0-24.noarch                                                                1/1

インストール済み:
  pgdg-redhat-repo-42.0-24.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 の名前                                                                      状態
(中略)
pgdg-centos8-sysupdates          PostgreSQL Supplementary ucommon RPMs for RHEL / Rocky 8 - x86_64                無効化
pgdg-common                      PostgreSQL common RPMs for RHEL / Rocky 8 - x86_64                               有効化
pgdg-common-srpm-testing         PostgreSQL common testing SRPMs for RHEL / Rocky 8 - x86_64                      無効化
pgdg-common-testing              PostgreSQL common testing RPMs for RHEL / Rocky 8 - x86_64                       無効化
pgdg-rhel8-extras                Extra packages to support some RPMs in the PostgreSQL RPM repo RHEL / Rocky 8 -  無効化
pgdg-source-common               PostgreSQL 12 for RHEL / Rocky 8 - x86_64 - Source                               無効化
pgdg10                           PostgreSQL 10 for RHEL / Rocky 8 - x86_64                                        有効化
pgdg10-debuginfo                 PostgreSQL 10 for RHEL / Rocky 8 - x86_64 - Debuginfo                            無効化
pgdg10-source                    PostgreSQL 10 for RHEL / Rocky 8 - x86_64 - Source                               無効化
pgdg11                           PostgreSQL 11 for RHEL / Rocky 8 - x86_64                                        有効化
pgdg11-debuginfo                 PostgreSQL 11 for RHEL / Rocky 8 - x86_64 - Debuginfo                            無効化
pgdg11-source                    PostgreSQL 11 for RHEL / Rocky 8 - x86_64 - Source                               無効化
pgdg11-source-updates-testing    PostgreSQL 11 for RHEL / Rocky 8 - x86_64 - Source update testing                無効化
pgdg11-updates-testing           PostgreSQL 11 for RHEL / Rocky 8 - x86_64 - Updates testing                      無効化
pgdg11-updates-testing-debuginfo PostgreSQL 11 for RHEL / Rocky 8 - x86_64 - Debuginfo                            無効化
pgdg12                           PostgreSQL 12 for RHEL / Rocky 8 - x86_64                                        有効化
pgdg12-debuginfo                 PostgreSQL 12 for RHEL / Rocky 8 - x86_64 - Debuginfo                            無効化
pgdg12-source                    PostgreSQL 12 for RHEL / Rocky 8 - x86_64 - Source                               無効化
pgdg12-source-updates-testing    PostgreSQL 12 for RHEL / Rocky 8 - x86_64 - Source update testing                無効化
pgdg12-updates-testing           PostgreSQL 12 for RHEL / Rocky 8 - x86_64 - Updates testing                      無効化
pgdg12-updates-testing-debuginfo PostgreSQL 12 for RHEL / Rocky 8 - x86_64 - Debuginfo                            無効化
pgdg13                           PostgreSQL 13 for RHEL / Rocky 8 - x86_64                                        有効化
pgdg13-debuginfo                 PostgreSQL 13 for RHEL / Rocky 8 - x86_64 - Debuginfo                            無効化
pgdg13-source-updates-testing    PostgreSQL 13 for RHEL / Rocky 8 - x86_64 - Source updates testing               無効化
pgdg13-updates-testing           PostgreSQL 13 for RHEL / Rocky 8 - x86_64 - Updates testing                      無効化
pgdg13-updates-testing-debuginfo PostgreSQL 13 for RHEL / Rocky 8 - x86_64 - Debuginfo                            無効化
pgdg14                           PostgreSQL 14 for RHEL / Rocky 8 - x86_64                                        有効化
pgdg14-debuginfo                 PostgreSQL 14 for RHEL / Rocky 8 - x86_64 - Debuginfo                            無効化
pgdg14-source-updates-testing    PostgreSQL 14 for RHEL / Rocky 8 - x86_64 - Source updates testing               無効化
pgdg14-updates-testing           PostgreSQL 14 for RHEL / Rocky 8 - x86_64 - Updates testing                      無効化
pgdg14-updates-testing-debuginfo PostgreSQL 14 for RHEL / Rocky 8 - x86_64 - Debuginfo                            無効化
pgdg15-updates-testing           PostgreSQL 15 for RHEL / Rocky 8 - x86_64 - Updates testing                      無効化
(中略)

こんな感じなので、今回使いたい pgdg13 以外を無効化します。

[root@localhost ~]# dnf config-manager --disable pgdg10
[root@localhost ~]# dnf config-manager --disable pgdg11
[root@localhost ~]# dnf config-manager --disable pgdg12
[root@localhost ~]# dnf config-manager --disable pgdg14
[root@localhost ~]# dnf repolist --all
(中略)

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

インストール

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

[root@localhost ~]# dnf install postgresql13 postgresql13-server
依存関係が解決しました。
========================================================================================================================
 パッケージ                         アーキテクチャー      バージョン                        リポジトリー          サイズ
========================================================================================================================
インストール:
 postgresql13                       x86_64                13.6-1PGDG.rhel8                  pgdg13                1.5 M
 postgresql13-server                x86_64                13.6-1PGDG.rhel8                  pgdg13                5.5 M
依存関係のインストール:
 libicu                             x86_64                60.3-2.el8_1                      baseos                8.8 M
 postgresql13-libs                  x86_64                13.6-1PGDG.rhel8                  pgdg13                415 k

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

ダウンロードサイズの合計: 16 M
インストール後のサイズ: 63 M
これでよろしいですか? [y/N]: y
パッケージのダウンロード:
(中略)
完了しました!
[root@localhost ~]#

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

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

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

特に変更しない場合、データは、/var/lib/pgsql/13/data/ に保存されます。OS公式で入れた場合とは異なるので注意が必要です。

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

[root@localhost ~]# vi /usr/lib/syst//system/postgresql-13.service
# Location of database directory
Environment=PGDATA=/var/lib/pgsql/13/data/

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

データベースの初期化

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

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

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

サービス

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

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

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

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

[root@localhost ~]# cp /var/lib/pgsql/13/data/postgresql.conf /var/lib/pgsql/13/data/postgresql.conf.org
[root@localhost ~]# vi /var/lib/pgsql/13/data/postgresql.conf
*** /var/lib/pgsql/13/data/postgresql.conf.org YYYY-mm-dd hh:ii:ss.000000000 +0900
--- /var/lib/pgsql/13/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 を書き換えることになりますが、* はすべてを許可になるため、接続元がわかっている場合は制限を掛けるなどしても良いでしょう。

次に接続するユーザーの認証方式などを変更します。

初期化の際に特に設定していないので、データベースにアクセスする方式が peer か scram-sha-256 になっています。プログラムから接続する場合になにかと不便なので、設定を変更します。

[root@localhost ~]# cp /var/lib/pgsql/13/data/pg_hba.conf /var/lib/pgsql/13/data/pg_hba.conf.org
[root@localhost ~]# vi /var/lib/pgsql/13/data/pg_hba.conf
*** /var/lib/pgsql/13/data/pg_hba.conf.org YYYY-mm-dd hh:ii:ss.000000000 +0900
--- /var/lib/pgsql/13/data/pg_hba.conf     YYYY-mm-dd hh:ii:ss.000000000 +0900
***************
*** 81,87 ****
        # TYPE  DATABASE        USER            ADDRESS                 METHOD

        # "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:
--- 81,87 ----
        # TYPE  DATABASE        USER            ADDRESS                 METHOD

        # "local" is for Unix domain socket connections only
!       local   all             all                                     md5
        # IPv4 local connections:
        host    all             all             127.0.0.1/32            scram-sha-256
        # IPv6 local connections:

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

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

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.5)
"help"でヘルプを表示します。

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