仕事においてインターネットへ接続できない閉鎖環境下において、PostgreSQL Clients および、pg_repack(クライアント側パッケージ)を導入する必要が生じたので、手順確認の意味を含めて導入してみる。
DBサーバの設定
まずは、接続先となるPostgreSQLのバージョンを確認する。
[postgres@localhost ~]$ psql
psql (17.11)
"help"でヘルプを表示します。
postgres=# select * from version();
version
------------------------------------------------------------------------------------------------------------
PostgreSQL 17.11 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 11.5.0 20240719 (Red Hat 11.5.0-14), 64-bit
(1 行)
次に拡張機能 pg_repack を有効化する。
DBサーバ側でpg_repackを有効化するためには、予めDBサーバに pg_repack パッケージのインストールが必要。
dnf install -y pg_repack_XX
XX:対象のPostgreSQLのバージョン番号を指定する
未インストールで、CREATE EXTENSION IF NOT EXISTS pg_repack; を実行すると下記エラーとなる。
【エラーメッセージ】
postgres=# CREATE EXTENSION IF NOT EXISTS pg_repack;
ERROR: 機能拡張"pg_repack" は利用できません
DETAIL: 機能拡張の制御ファイル"/usr/pgsql-17/share/extension/pg_repack.control"をオープンできませんでした: そのようなファイ ルやディレクトリはありません
HINT: PostgreSQLが稼働しているシステムで、事前に機能拡張がインストールされている必要があります。
postgres=# \dx
インストール済みの拡張一覧
名前 | バージョン | スキーマ | 説明
---------+------------+------------+--------------------------------------------
amcheck | 1.4 | public | functions for verifying relation integrity
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
(2 行)
postgres=#
postgres=#
postgres=# CREATE EXTENSION IF NOT EXISTS pg_repack;
CREATE EXTENSION
postgres=#
postgres=# \dx
インストール済みの拡張一覧
名前 | バージョン | スキーマ | 説明
-----------+------------+------------+--------------------------------------------------------------
amcheck | 1.4 | public | functions for verifying relation integrity
pg_repack | 1.5.3 | public | Reorganize tables in PostgreSQL databases with minimal locks
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
(3 行)
postgres=#
クライアントサーバの設定
クライアント側のOSバージョンを確認する。
これにより、クライアント側に導入できるPostgreSQL Clientsのパッケージが決まる。
[root@pgclients100g ~]# cat /etc/redhat-release
Rocky Linux release 9.6 (Blue Onyx)
PostgreSQL公式のYUMリポジトリから必要なパッケージを選定する。
・postgresql17-libs-17.X:共通ライブラリ

本来、DBサーバのバージョン(17.11)に合わせるべきだが、今回は17.7を使用する。
pg_repackは、DBサーバとクライアントでパッケージは分かれていないため、DBサーバと同じパッケージを導入する。
改めて必要なパッケージを整理すると、以下になる。
クライアント側 パッケージ
・PostgreSQL Clients
- postgresql17-libs-17.7-3PGDG.rhel9.6.x86_64.rpm
‐ postgresql17-17.7-3PGDG.rhel9.6.x86_64.rpm
・pg_repack
‐ pg_repack_17-1.5.3-1PGDG.rhel9.x86_64.rpm
クライアント側サーバに対象のパッケージを配置したら、インストールする。
今回は、rpmコマンドを用いてインストールする。
[root@pgclients100g psql_client_rpm]# rpm -ivh ./postgresql17-libs-17.7-3PGDG.rhel9.6.x86_64.rpm
警告: ./postgresql17-libs-17.7-3PGDG.rhel9.6.x86_64.rpm: ヘッダー V4 RSA/SHA256 Signature、鍵 ID 08b40d20: NOKEY
Verifying... ################################# [100%]
準備しています... ################################# [100%]
更新中 / インストール中...
1:postgresql17-libs-17.7-3PGDG.rhel################################# [100%]
[root@pgclients100g psql_client_rpm]#
[root@pgclients100g psql_client_rpm]# rpm -ivh ./postgresql17-17.7-3PGDG.rhel9.6.x86_64.rpm
警告: ./postgresql17-17.7-3PGDG.rhel9.6.x86_64.rpm: ヘッダー V4 RSA/SHA256 Signature、鍵 ID 08b40d20: NOKEY
Verifying... ################################# [100%]
準備しています... ################################# [100%]
更新中 / インストール中...
1:postgresql17-17.7-3PGDG.rhel9.6 ################################# [100%]
[root@pgclients100g psql_client_rpm]#
[root@pgclients100g psql_client_rpm]# rpm -ivh ./pg_repack_17-1.5.3-1PGDG.rhel9.x86_64.rpm
警告: ./pg_repack_17-1.5.3-1PGDG.rhel9.x86_64.rpm: ヘッダー V4 RSA/SHA256 Signature、鍵 ID 08b40d20: NOKEY
Verifying... ################################# [100%]
準備しています... ################################# [100%]
更新中 / インストール中...
1:pg_repack_17-1.5.3-1PGDG.rhel9 ################################# [100%]
[root@pgclients100g psql_client_rpm]#
インストールが完了したので、コマンドで確認。
[root@pgclients100g psql_client_rpm]# rpm -qa | grep -E "postgresql17|pg_repack"
postgresql17-libs-17.7-3PGDG.rhel9.6.x86_64
postgresql17-17.7-3PGDG.rhel9.6.x86_64
pg_repack_17-1.5.3-1PGDG.rhel9.x86_64
[root@pgclients100g psql_client_rpm]#
インストールしたpg_repackのバージョンを確認する。
[root@pgclients100g psql_client_rpm]# /usr/pgsql-17/bin/pg_repack --version
pg_repack 1.5.3
[root@pgclients100g psql_client_rpm]#
pg_repackの動作確認。
[root@pgclients100g psql_client_rpm]# /usr/pgsql-17/bin/pg_repack --dry-run -h xxx.xxx.56.198 -p 5432 -d postgres -U postgres
INFO: Dry run enabled, not executing repack
ERROR: could not connect to database: "xxx.xxx.56.198"、ポート5432のサーバーへの接続に失敗しました: 接続を拒否されました
サーバーはそのホスト上で稼働していてTCP/IP接続を受け付けていますか?
[root@pgclients100g psql_client_rpm]#
接続NGでエラーとなった。
DBサーバの接続許可設定(postgresql.conf)
通信経路を調査してみると、ping も traceroute も、問題はないが、nc -zv 192.168.56.198 5432 で Connection refused.になった。
どうやらPostgreSQLはデフォルトでは、localhost(自分自身)からの接続のみ許可しているらしい。まずは設定を確認する。
【確認項目】
PostgreSQL設定ファイル:/var/lib/pgsql/17/data/postgresql.conf
listen_addresses:接続を許可する接続元IP
[root@localhost ~]# ls -ltr /var/lib/pgsql/17/data/postgresql.conf
-rw-------. 1 postgres postgres 30881 4月 12 2026 /var/lib/pgsql/17/data/postgresql.conf
[root@localhost ~]# cat /var/lib/pgsql/17/data/postgresql.conf | grep listen
#listen_addresses = 'localhost' # what IP address(es) to listen on;
[root@localhost ~]#
~~ 中略 ~~
[root@localhost ~]# su - postgres
[postgres@localhost ~]$ psql
psql (17.12)
"help"でヘルプを表示します。
postgres=# SHOW listen_addresses;
listen_addresses
------------------
localhost
(1 行)
postgres=#
確認したところコメントアウトされていたが、デフォルトでは localhost(ローカル接続)のみ許可しているらしい。これを変更する。
今回はVirtualbox環境での検証のため、「*」 を設定する。
[root@localhost ~]# cp -p /var/lib/pgsql/17/data/postgresql.conf /var/lib/pgsql/17/data/postgresql.conf_`date +%Y%m%d`
[root@localhost ~]# ls -ltr /var/lib/pgsql/17/data/postgresql.conf*
-rw-------. 1 postgres postgres 30881 4月 12 2026 /var/lib/pgsql/17/data/postgresql.conf_20261115
-rw-------. 1 postgres postgres 30881 4月 12 2026 /var/lib/pgsql/17/data/postgresql.conf
[root@localhost ~]#
[root@localhost ~]# vi /var/lib/pgsql/17/data/postgresql.conf
[root@localhost ~]#
[root@localhost ~]# diff /var/lib/pgsql/17/data/postgresql.conf /var/lib/pgsql/17/data/postgresql.conf_`date +%Y%m%d`
61d60
< listen_addresses = '*'
[root@localhost ~]# sdiff -w 180 /var/lib/pgsql/17/data/postgresql.conf /var/lib/pgsql/17/data/postgresql.conf_`date +%Y%m%d` | grep -A 5 -B 5 "listen_address"
# CONNECTIONS AND AUTHENTICATION # CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
# - Connection Settings - # - Connection Settings -
#listen_addresses = 'localhost' # what IP address(es) to listen on; #listen_addresses = 'localhost' # what IP address(es) to listen on;
listen_addresses = '*' <
# comma-separated list of addresses; # comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all # defaults to 'localhost'; use '*' for all
# (change requires restart) # (change requires restart)
#port = 5432 # (change requires restart) #port = 5432 # (change requires restart)
max_connections = 100 # (change requires restart) max_connections = 100 # (change requires restart)
変更を反映するために、PostgreSQLを再起動する。
[root@localhost ~]# systemctl restart postgresql-17.service
[root@localhost ~]# systemctl status postgresql-17.service
● postgresql-17.service - PostgreSQL 17 database server
Loaded: loaded (/usr/lib/systemd/system/postgresql-17.service; enabled; preset: disabled)
Active: active (running) since Sun 2026-11-15 09:43:55 JST; 6s ago
Docs: https://www.postgresql.org/docs/17/static/
Process: 2322 ExecStartPre=/usr/pgsql-17/bin/postgresql-17-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
Main PID: 2328 (postgres)
Tasks: 7 (limit: 22968)
Memory: 17.9M (peak: 18.0M)
CPU: 143ms
CGroup: /system.slice/postgresql-17.service
tq2328 /usr/pgsql-17/bin/postgres -D /var/lib/pgsql/17/data/
tq2329 "postgres: logger "
tq2330 "postgres: checkpointer "
tq2331 "postgres: background writer "
tq2333 "postgres: walwriter "
tq2334 "postgres: autovacuum launcher "
mq2335 "postgres: logical replication launcher "
11月 15 09:43:55 localhost.localdomain systemd[1]: Starting PostgreSQL 17 database server...
11月 15 09:43:55 localhost.localdomain postgres[2328]: 2026-11-15 09:43:55.252 JST [2328] LOG: ログ出力をログ収集プロセスにリダイレクトしています
11月 15 09:43:55 localhost.localdomain postgres[2328]: 2026-11-15 09:43:55.252 JST [2328] ヒント: ここからのログ出力はディレクトリ"log"に現れます。
11月 15 09:43:55 localhost.localdomain systemd[1]: Started PostgreSQL 17 database server.
[root@localhost ~]#
[root@localhost ~]# su - postgres
[postgres@localhost ~]$ psql
psql (17.12)
"help"でヘルプを表示します。
postgres=# SHOW listen_addresses;
listen_addresses
------------------
*
(1 行)
postgres=#
pg_repackの動作検証(リトライ)
再度、pg_repackの動作検証を行う。
[root@pgclients100g ~]# /usr/pgsql-17/bin/pg_repack --dry-run -h xxx.xxx.56.198 -p 5432 -d postgres -U postgres
INFO: Dry run enabled, not executing repack
ERROR: could not connect to database: "xxx.xxx.56.198"、ポート5432のサーバーへの接続に失敗しました: FATAL: pg_hba.conf にホスト"xxx.xxx.56.7"、ユーザー"postgres"、データベース"postgres, 暗号化なし用のエントリがありません
[root@pgclients100g ~]#
エラー内容が変わった。
今度は、認証まわりの設定がないらしい。
DBサーバのクライアント認証(pg_hba.conf)
当該ファイルはクライアント認証に関する設定ファイルで、クライアントのIP、接続先のDB、接続時のユーザ等の情報を定義する。
認証は設定の上から1行ずつ判定され、条件に一致したものがある場合、後続の設定は使用されないため注意が必要だ。
詳細な記載フォーマットは、以下の記事が分かりやすい。
末尾に今回の定義情報を追加し、設定ファイルを読み込ませるためにリロードする。
[root@localhost ~]# ls -ltr /var/lib/pgsql/17/data/pg_hba.conf
-rw-------. 1 postgres postgres 5499 4月 12 2026 /var/lib/pgsql/17/data/pg_hba.conf
[root@localhost ~]# cp -p /var/lib/pgsql/17/data/pg_hba.conf /var/lib/pgsql/17/data/pg_hba.conf_`date +%Y%m%d`
[root@localhost ~]# ls -ltr /var/lib/pgsql/17/data/pg_hba.conf*
-rw-------. 1 postgres postgres 5499 4月 12 2026 /var/lib/pgsql/17/data/pg_hba.conf_20261115
-rw-------. 1 postgres postgres 5499 4月 12 2026 /var/lib/pgsql/17/data/pg_hba.conf
[root@localhost ~]# vi /var/lib/pgsql/17/data/pg_hba.conf
[root@localhost ~]# diff /var/lib/pgsql/17/data/pg_hba.conf /var/lib/pgsql/17/data/pg_hba.conf_`date +%Y%m%d`
123d122
< host all all xxx.xxx.56.7/32 scram-sha-256
[root@localhost ~]# systemctl reload postgresql-17.service
pg_repackの動作検証(設定変更後のリトライ)
これでDBへ接続できるはずなので、再度実行する。
[root@pgclients100g ~]# /usr/pgsql-17/bin/pg_repack --dry-run -h xxx.xxx.56.198 -p 5432 -d postgres -U postgres
INFO: Dry run enabled, not executing repack
Password:
[root@pgclients100g ~]# /usr/pgsql-17/bin/pg_repack --dry-run -h xxx.xxx.56.198 -p 5432 -d postgres -U postgres -e
INFO: Dry run enabled, not executing repack
Password:
LOG: (query) SET search_path TO pg_catalog, pg_temp, public
LOG: (query) SET search_path TO pg_catalog, pg_temp, public
LOG: (query) select repack.version(), repack.version_sql()
LOG: (query) SET statement_timeout = 0
LOG: (query) SET search_path = pg_catalog, pg_temp, public
LOG: (query) SET client_min_messages = warning
LOG: (query) SELECT t.*, coalesce(v.tablespace, t.tablespace_orig) as tablespace_dest FROM repack.tables t, (VALUES ($1::text)) as v (tablespace) WHERE pkid IS NOT NULL ORDER BY t.relname, t.schemaname
LOG: (param:0) = (null)
[root@pgclients100g ~]#
Dry runを実行しても何も出力されなかったので、-e オプションをつけてpg_repackが内部で実行しているSQLを出力させたが、postgresデータベース内にユーザ作成のテーブルが存在しないため、何も出力されなかったようだ。
これで動作確認まで完了できた。
補足
・pg_repackとは


