公開日:2026/9/10 0:00:00

Amazon Linux 2023 Section 3 nginx + PostgreSQL + PHP 構成

Section 3.1 PostgreSQL

About

PostgreSQLのインストールと設定手順を残しています。 AWS環境であればRDSを使うのでClientはインストールするにしても、Serverまでというケースは少ない気もします。

環境は、Section 1.1Section 1.3で作成したvirtualboxの環境で実行しています。 起動後、rootアカウントになって作業を進めます。

バージョンを調べる

リポジトリに登録されているものからパッケージ名とバージョンを指定してインストールすることになります。 まずは、提供されているかどうかを調べます。

[root@localhost ~]# dnf search postgresql*server
============================================= 名前 一致: postgresql*server =============================================
postgresql15-server.x86_64 : The programs needed to create and run a PostgreSQL server
postgresql16-server.x86_64 : The programs needed to create and run a PostgreSQL server
postgresql17-server.x86_64 : The programs needed to create and run a PostgreSQL server
postgresql18-server.x86_64 : The programs needed to create and run a PostgreSQL server
============================================= 概要 一致: postgresql*server =============================================
postgresql15-private-devel.x86_64 : PostgreSQL development header files for this build of PostgreSQL server
postgresql16-private-devel.x86_64 : PostgreSQL development header files for this build of PostgreSQL server
postgresql17-private-devel.x86_64 : PostgreSQL development header files for this build of PostgreSQL server
postgresql18-private-devel.x86_64 : PostgreSQL development header files for this build of PostgreSQL server
[root@localhost ~]#

複数のバージョンが登録されていることがわかります。 バージョン管理はメジャーのみで、マイナーは自動アップデートという感じに見えます。 現時点で最新の 18 のマイナーリリースを確認します。

[root@localhost ~]# dnf repoquery postgresql18
postgresql18-0:18.3-1.amzn2023.0.1.x86_64
postgresql18-0:18.4-1.amzn2023.0.1.x86_64
postgresql18-0:18.6-1.amzn2023.0.1.x86_64
[root@localhost ~]#

18.6 になりそうなのでこれでインストールを進めていきます。

インストール

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

[root@localhost ~]# dnf -y install postgresql18 postgresql18-server
依存関係が解決しました。
========================================================================================================================
 パッケージ                            アーキテクチャー   バージョン                      リポジトリー            サイズ
========================================================================================================================
インストール:
 postgresql18                          x86_64             18.6-1.amzn2023.0.1             amazonlinux             2.0 M
 postgresql18-server                   x86_64             18.6-1.amzn2023.0.1             amazonlinux             7.5 M
依存関係のインストール:
 libicu                                x86_64             67.1-7.amzn2023.0.4             amazonlinux             9.6 M
 postgresql18-private-libs             x86_64             18.6-1.amzn2023.0.1             amazonlinux             161 k

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

(中略)

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

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

データベースの初期化

DBの初期化します。この初期化後にDB本体が保存される場所が作成されます。 内部で使う文字コードを強制指定したいので少し変わった方法で初期化コマンドを実行します。

[root@localhost ~]# PGSETUP_INITDB_OPTIONS="--encoding=UTF-8" /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コンテキストが変わってしまいます。enforceモードではないのでサービスが起動できないということにはならないと思いますが、トラブルのものとになるようなことはしないほうがいいでしょう。

SELinuxのラベル変更方法

/var/lib/pgsql のコンテキストはOSで登録済みなので、設定を復元すればよいということになります。 なので、restorecon を使用して復元すれば対応できます。

[root@localhost ~]# restorecon -v -r /var/lib/pgsql

サービス

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

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