What's?
タイトルどおり、AlmaLinx 8にPostgreSQLのyum
リポジトリーを使用してPostgreSQL 14をインストールしてみます。
環境
今回の環境は、こちらです。
$ cat /etc/redhat-release
AlmaLinux release 8.5 (Arctic Sphynx)
$ uname -srvmpio
Linux 4.18.0-348.23.1.el8_5.x86_64 #1 SMP Wed Apr 27 05:47:31 EDT 2022 x86_64 x86_64 x86_64 GNU/Linux
インストール方法を確認する
最初に、インストール方法を確認してみます。
こちらのページで「Linux」を選択して、現れた選択肢から「Red Hat/Rocky/CentOS」を選択。
すると、こちらのページに移るので、手順に沿って進めていけばよさそうです。
このページが対象としているのは、以下なのですがAlmaLinux 8でもまあ大丈夫かな、と。
- Red Hat Enterprise Linux
- Rocky Linux
- CentOS 7または6
- Fedora
- Oracle Linux
ちなみ、RPMを探すならこちらです。
PostgreSQL 14をyumリポジトリーからインストールする
では、インストールしていきましょう。
yum
リポジトリーをインストール。
$ sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
次に、デフォルトのPostgreSQLモジュールを無効にします。
$ sudo dnf -qy module disable postgresql
ちなみに、無効にしなかった場合は追加したyum
リポジトリーのPostgreSQLが見えません。
$ sudo dnf install postgresql14-server
Last metadata expiration check: 0:00:05 ago on 2022年04月29日 22時04分39秒.
All matches were filtered out by modular filtering for argument: postgresql14-server
Error: Unable to find a match: postgresql14-server
postgresql14-server
をインストール。
$ sudo dnf install postgresql14-server
今回のバージョンは、PostgreSQL 14.2でした。
$ psql --version
psql (PostgreSQL) 14.2
パッケージの情報。
$ dnf info postgresql14-server
Last metadata expiration check: 0:03:34 ago on 2022年04月29日 22時02分59秒.
Installed Packages
Name : postgresql14-server
Version : 14.2
Release : 1PGDG.rhel8
Architecture : x86_64
Size : 23 M
Source : postgresql14-14.2-1PGDG.rhel8.src.rpm
Repository : @System
From repo : pgdg14
Summary : The programs needed to create and run a PostgreSQL server
URL : https://www.postgresql.org/
License : PostgreSQL
Description : PostgreSQL is an advanced Object-Relational database management system (DBMS).
: The postgresql14-server package contains the programs needed to create
: and run a PostgreSQL server, which will in turn allow you to create
: and maintain PostgreSQL databases.
続いて、データベースの初期化を行います。
$ sudo PGSETUP_INITDB_OPTIONS='--encoding=UTF-8 --no-locale' /usr/pgsql-14/bin/postgresql-14-setup initdb
PGSETUP_INITDB_OPTIONS
環境変数を使って、エンコーディングはUTF-8、ロケールはなしとしています。
ロケールがC
になりました。
$ sudo grep lc_ /var/lib/pgsql/14/data/postgresql.conf
lc_messages = 'C' # locale for system error message
lc_monetary = 'C' # locale for monetary formatting
lc_numeric = 'C' # locale for number formatting
lc_time = 'C' # locale for time formatting
これをドキュメント通りにsudo /usr/pgsql-14/bin/postgresql-14-setup initdb
とやってしまうと、ロケールが現在の環境のものになってしまいます。
$ sudo /usr/pgsql-14/bin/postgresql-14-setup initdb
Initializing database ... OK
$ sudo grep lc_ /var/lib/pgsql/14/data/postgresql.conf
# - Locale and Formatting -
lc_messages = 'ja_JP.utf8' # locale for system error message
lc_monetary = 'ja_JP.utf8' # locale for monetary formatting
lc_numeric = 'ja_JP.utf8' # locale for number formatting
lc_time = 'ja_JP.utf8' # locale for time formatting
自動起動の有効化。
$ sudo systemctl enable postgresql-14
ステータス。
$ sudo systemctl status postgresql-14
● postgresql-14.service - PostgreSQL 14 database server
Loaded: loaded (/usr/lib/systemd/system/postgresql-14.service; enabled; vendor preset: disabled)
Active: inactive (dead)
Docs: https://www.postgresql.org/docs/14/static/
起動。
$ sudo systemctl start postgresql-14
ステータスを確認。
$ sudo systemctl status postgresql-14
● postgresql-14.service - PostgreSQL 14 database server
Loaded: loaded (/usr/lib/systemd/system/postgresql-14.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2022-04-29 22:10:52 JST; 10s ago
Docs: https://www.postgresql.org/docs/14/static/
Process: 4022 ExecStartPre=/usr/pgsql-14/bin/postgresql-14-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
Main PID: 4027 (postmaster)
Tasks: 8 (limit: 12442)
Memory: 16.6M
CGroup: /system.slice/postgresql-14.service
├─4027 /usr/pgsql-14/bin/postmaster -D /var/lib/pgsql/14/data/
├─4029 postgres: logger
├─4031 postgres: checkpointer
├─4032 postgres: background writer
├─4033 postgres: walwriter
├─4034 postgres: autovacuum launcher
├─4035 postgres: stats collector
└─4036 postgres: logical replication launcher
4月 29 22:10:52 localhost.localdomain systemd[1]: Starting PostgreSQL 14 database server...
4月 29 22:10:52 localhost.localdomain postmaster[4027]: 2022-04-29 22:10:52.411 JST [4027] LOG: redirecting log output to logging collector process
4月 29 22:10:52 localhost.localdomain postmaster[4027]: 2022-04-29 22:10:52.411 JST [4027] HINT: Future log output will appear in directory "log".
4月 29 22:10:52 localhost.localdomain systemd[1]: Started PostgreSQL 14 database server.
データ等は、/var/lib/pgsql/14
ディレクトリにあります。
$ sudo ls -l /var/lib/pgsql/14
total 8
drwx------. 2 postgres postgres 6 2月 9 17:13 backups
drwx------. 20 postgres postgres 4096 4月 29 22:10 data
-rw-------. 1 postgres postgres 1260 4月 29 22:09 initdb.log
動作確認
動作確認しましょう。
postgres
ユーザーにスイッチして
$ sudo su - postgres
psql
でログインして、バージョン確認。
$ psql
psql (14.2)
"help"でヘルプを表示します。
postgres=# select version();
version
--------------------------------------------------------------------------------------------------------
PostgreSQL 14.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-4), 64-bit
(1 行)
ユーザーとデータベースの作成。
postgres=# create user myuser password 'password';
CREATE ROLE
postgres=# create database example owner myuser;
CREATE DATABASE
ユーザーを作成したので、ネットワーク越しにアクセスしてみたいのですが-h
オプションを使ってアクセスすると、接続が拒否されます。
※サーバーのIPアドレスは、192.168.33.10
です
$ psql -U myuser -h 192.168.33.10 example
psql: エラー: "192.168.33.10"、ポート5432でのサーバーへの接続に失敗しました: 接続を拒否されました
サーバーはそのホストで動作していて、TCP/IP接続を受け付けていますか?
ネットワーク接続も、ローカルのみからの許可ですね。
$ ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 127.0.0.1:5432 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 [::1]:5432 [::]:*
接続できるように、PostgreSQLの設定変更を行いましょう。
PostgreSQLの設定を変更する
まず、/var/lib/pgsql/14/data/postgresql.conf
のlisten_addresses
を確認して
#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------
# - Connection Settings -
#listen_addresses = 'localhost' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
コメントアウトを解除して、他のサーバーからの接続を受け付けるように変更します。
#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------
# - Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
次に、/var/lib/pgsql/14/data/pg_hba.conf
を変更します。
host all all 192.168.33.0/24 scram-sha-256
PosgreSQLを再起動。
$ sudo systemctl restart postgresql-14
これで、アクセスできるようになりました。
$ psql -U myuser -h 192.168.33.10 example
ユーザー myuser のパスワード:
psql (14.2)
"help"でヘルプを表示します。
example=>