環境構築からWEBアプリ開発・スマホアプリ開発まで。ときには動画制作やゲームも。

supilog
すぴろぐ

AlmaLinux9でPostgreSQL17.4をインストールする

AlmaLinux9でPostgreSQL17.4をインストールする

インストール

公式手順(https://www.postgresql.org/download/linux/redhat)に従い、インストールしてみる。

# Install the repository RPM:
$ sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm

# Disable the built-in PostgreSQL module:
$ sudo dnf -qy module disable postgresql

# Install PostgreSQL:
$ sudo dnf install -y postgresql17-server

# Optionally initialize the database and enable automatic start:
$ sudo /usr/pgsql-17/bin/postgresql-17-setup initdb
$ sudo systemctl enable postgresql-17
$ sudo systemctl start postgresql-17

確認してみる。

$ systemctl status postgresql-17
-----
 postgresql-17.service - PostgreSQL 17 database server
     Loaded: loaded (/usr/lib/systemd/system/postgresql-17.service; enabled; preset: disabled)
     Active: active (running) since Wed 2025-03-05 14:00:24 JST; 59s ago
       Docs: https://www.postgresql.org/docs/17/static/
    Process: 78569 ExecStartPre=/usr/pgsql-17/bin/postgresql-17-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
   Main PID: 78574 (postgres)
      Tasks: 7 (limit: 4676)
     Memory: 17.7M
        CPU: 84ms
     CGroup: /system.slice/postgresql-17.service
             ├─78574 /usr/pgsql-17/bin/postgres -D /var/lib/pgsql/17/data/
             ├─78576 "postgres: logger "
             ├─78578 "postgres: checkpointer "
             ├─78579 "postgres: background writer "
             ├─78581 "postgres: walwriter "
             ├─78582 "postgres: autovacuum launcher "
             └─78583 "postgres: logical replication launcher "

 3月 05 14:00:24 alma9.supilog.local systemd[1]: Starting PostgreSQL 17 database server...
 3月 05 14:00:24 alma9.supilog.local postgres[78574]: 2025-03-05 14:00:24.834 JST [78574] LOG:  ログ出力をロ>
 3月 05 14:00:24 alma9.supilog.local postgres[78574]: 2025-03-05 14:00:24.834 JST [78574] ヒント:  ここから >
 3月 05 14:00:24 alma9.supilog.local systemd[1]: Started PostgreSQL 17 database server.
-----

初回ログイン

初期設定では、localからの接続がpeer設定になっている。PostgreSQLに保存されているユーザーとユーザーが一致している状態であれば、パスワードなしでログイン出来るというもの。

postgresユーザーになってPostgreSQLにログイン

$ sudo su - postgres
$ psql

-----
psql (17.4)
"help"でヘルプを表示します。

postgres=#
-----

scram-sha-256認証に変更する

認証方法を指定

# 認証方法を指定
$ vi /var/lib/pgsql/17/data/postgresql.conf
-----
password_encryption = scram-sha-256	# scram-sha-256 or md5
-----

# 再起動
$ systemctl restart postgresql-17.service

postgresユーザーのパスワードを設定

※PostgreSQLにログインする手順は省略

postgres=# ALTER USER postgres WITH encrypted PASSWORD 'hogehoge';

接続設定

# scram-sha-256に変更
$ vi /var/lib/pgsql/17/data/pg_hba.conf
-----
local   all             all                                     peer
local   replication     all                                     peer
↓
local   all             all                                     scram-sha-256
local   replication     all                                     scram-sha-256
-----

# 再起動
$ systemctl restart postgresql-17.service

この作業を終えると、PostgreSQLにログインする際にパスワードが必要になるので注意。

DB作成&ユーザー作成&権限設定

※PostgreSQLにログインする手順は省略

# PostgreSQLにログイン後、コマンドを実行
postgres=# create database supilog;
CREATE DATABASE
postgres=# create user supiloguser with encrypted password 'password';
CREATE ROLE
postgres=# grant all privileges on database supilog to supiloguser;
GRANT

接続確認

$ psql -U supiloguser supilog

ローカルからログイン出来ることを確認する。

その他設定

PostgreSQLのポート番号を変更する

微々たるものではあるが、セキュリティ的には変更しておいた方がやや良い。ダイナミック/プライベートポート番号(49152~65535)から任意に決定する。

#port = 5432                            # (change requires restart)
↓
port = 49152                            # (change requires restart)

外部接続を可能にする

認証設定
host    all             all             127.0.0.1/32            scram-sha-256
↓
host    all             all             0.0.0.0/0            scram-sha-256
インターフェース設定

PostgreSQLサーバーのIPアドレスを追加することで、ネットワーク経由のアクセスを受け取ることが出来ます。【注意】接続元IPアドレスを制限するものではない。間違えているWEBサイトがちらほら。

listen_addresses = 'localhost'		# what IP address(es) to listen on;
↓
listen_addresses = 'localhost,<IPアドレス>'		# what IP address(es) to listen on

接続元IPアドレスを制限する

host    all             all             0.0.0.0/0            scram-sha-256
↓
host    all             all             <IPアドレス>/32            scram-sha-256

※「外部接続を可能にする」にて、「0.0.0.0/0」に変更したのを想定した記述です。