データベース - PostgreSQL - postgreSQL 16 → 18


クラウディア 


1. 概要
2. バックアップ・サーバ停止
3. postgreSQL 18 インストール
4. リストア
5. サーバ起動
6. 設定
7. 参考サイト

1. 概要

 サポート期限は、まだあるはずなのですが、事情がありまして・・・。  というのは、「Laravel」の「cache」をクリアしようとしたら下記のエラーになりました。

sudo -u www php /path/artisan cache:clear

ld-elf.so.1: /usr/local/lib/php/20240924/pdo_pgsql.so: Undefined symbol "PQclosePrepared"
*** Error code 1
 「Gemini」伍長に相談してみたところ、「php84-pdo_pgsql」「php84-pgsql」を再インストールしなさいとのことで、再インストールの「make」を行うと、これがエラーになる。

/usr/ports/databases/php84-pgsql/work/php-8.4.27/ext/pgsql/pgsql.c:6392:14: warning: call to undeclared function 'PQsetChunkedRowsMode'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
 6392 |         RETURN_BOOL(PQsetChunkedRowsMode(link->conn, (int)size) == 1);
      |                     ^
4 warnings and 2 errors generated.
*** [pgsql.lo] Error code 1

make: stopped making "all" in /usr/ports/databases/php84-pgsql/work/php-8.4.27/ext/pgsql
make: 1 error

make: stopped making "all" in /usr/ports/databases/php84-pgsql/work/php-8.4.27/ext/pgsql
===> Compilation failed unexpectedly.
Try to set MAKE_JOBS_UNSAFE=yes and rebuild before reporting the failure to
the maintainer.
*** Error code 1

Stop.
make: stopped making "all" in /usr/ports/databases/php84-pgsql
 結局のところ、エラーになっている「PQsetChunkedRowsMode」「PQclosePrepared」「PQclosePortal」などのモジュールが、「libpq」以降でないと存在しないらしい。  で、それが登場するのが、「PostgreSQL 17」以降らしいのです。  2026年11月14日現在、「ports」に存在するのが。

/usr/ports/databases/postgresql14-server
/usr/ports/databases/postgresql15-server
/usr/ports/databases/postgresql16-server
/usr/ports/databases/postgresql17-server
/usr/ports/databases/postgresql18-server
/usr/ports/databases/postgresql19-server
 でして、この際、「PostgreSQL 18」にしてみようという算段です。  そういや、「FreeBSD 14」の「ABI」を「FreeBSD 15」へアップグレードした際、「pkg」でアップグレードしようとすると、「PostgreSQL 18」をインストールしようとしておったな。

2. バックアップ・サーバ停止

 以降の、全項目、「root」ユーザ権限で。  現状のデータをバックアップ(そこまで大きくないので、テキストファイルに保存可能なのだ)。

sudo su postgres -c "pg_dumpall --column-inserts > pg_dumpall.sql"
 サーバ停止。

service postgresql stop
 データベース全体をバックアップ。

cd /var/db/postgres

cp -R data16 data16-20260101-backup
 バックアップのファイル名は、ここではその日の名前にしました。  で、今回は、リストアを高速に行うために、アンインストールする「PostgreSQL 16」のバイナリとライブラリが必要なため、退避しておきます。

mkdir -pv /tmp/pgsql16/bin /tmp/pgsql16/lib

cp -Rp /usr/local/bin/* /tmp/pgsql16/bin/

cp -Rp /usr/local/lib/postgresql /tmp/pgsql16/lib/

mkdir -p /tmp/pgsql16/share

ln -s /usr/local/share/postgresql /tmp/pgsql16/share/postgresql

3. postgreSQL 18 インストール

 「postgresql18-server」のインストールには、「llvm19」が必要なようです。  「llvm19」は、アップグレードの際は、たいてい既にインストールされているかと思いますが、インストールされていなければ、相当時間がかかります。  もしインストールされていないようであれば、「postgresql18-server」のインストール前に、「llvm19」をインストールしておいた方がよいかと思います。  コンフィグレーションを先に設定しておきます。

cd /usr/ports/databases/postgresql18-server/

make config
 オプションは、デフォルトのままでよいかと思います。

 「server」「slient」をアンインストールしておきます。


cd /usr/ports/databases/postgresql16-client/

make deinstall clean

cd /usr/ports/databases/postgresql16-server/

make deinstall clean
 その上で。

cd /usr/ports/databases/postgresql18-server/

make NO_DIALOG=yes

make install clean

4. リストア

 今回は、リストアを先にして、その後、サーバを起動します。  データベースを初期化しますが、少し工夫が必要です。

su - postgres -c "/usr/local/bin/initdb -D /var/db/postgres/data18 --no-data-checksums"
 下記のメッセージが、表示されます。

The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "C.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

creating directory /var/db/postgres/data18 ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default "max_connections" ... 100
selecting default "shared_buffers" ... 128MB
selecting default time zone ... Japan
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections
initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    /usr/local/bin/pg_ctl -D /var/db/postgres/data18 -l logfile start
 データベースは。

/var/db/postgres/data18/
 に作成されます。  「PostgreSQL 16」のデータを「PostgreSQL 18」のものへアップグレード。  まずチェック。

su - postgres -c "LD_LIBRARY_PATH=/tmp/pgsql16/lib:/tmp/pgsql16/lib/postgresql /usr/local/bin/pg_upgrade \
  --old-bindir=/tmp/pgsql16/bin \
  --new-bindir=/usr/local/bin \
  --old-datadir=/var/db/postgres/data16 \
  --new-datadir=/var/db/postgres/data18 \
  --check"
 この結果。

Performing Consistency Checks
-----------------------------
Checking cluster versions                                     ok
Checking database connection settings                         ok
Checking database user is the install user                    ok
Checking for prepared transactions                            ok
Checking for contrib/isn with bigint-passing mismatch         ok
Checking data type usage                                      ok
Checking for not-null constraint inconsistencies              ok
Checking for presence of required libraries                   ok
Checking database user is the install user                    ok
Checking for prepared transactions                            ok
Checking for new cluster tablespace directories               ok

*Clusters are compatible*
 となれば、データのアップグレード可能です。  アップグレードの実行。

su - postgres -c "LD_LIBRARY_PATH=/tmp/pgsql16/lib:/tmp/pgsql16/lib/postgresql /usr/local/bin/pg_upgrade \
  --old-bindir=/tmp/pgsql16/bin \
  --new-bindir=/usr/local/bin \
  --old-datadir=/var/db/postgres/data16 \
  --new-datadir=/var/db/postgres/data18"
 今度は、もっと長いです。

Performing Consistency Checks
-----------------------------
Checking cluster versions                                     ok
Checking database connection settings                         ok
Checking database user is the install user                    ok
Checking for prepared transactions                            ok
Checking for contrib/isn with bigint-passing mismatch         ok
Checking data type usage                                      ok
Checking for not-null constraint inconsistencies              ok
Creating dump of global objects                               ok
Creating dump of database schemas
                                                              ok
Checking for presence of required libraries                   ok
Checking database user is the install user                    ok
Checking for prepared transactions                            ok
Checking for new cluster tablespace directories               ok

If pg_upgrade fails after this point, you must re-initdb the
new cluster before continuing.

Performing Upgrade
------------------
Setting locale and encoding for new cluster                   ok
Analyzing all rows in the new cluster                         ok
Freezing all rows in the new cluster                          ok
Deleting files from new pg_xact                               ok
Copying old pg_xact to new server                             ok
Setting oldest XID for new cluster                            ok
Setting next transaction ID and epoch for new cluster         ok
Deleting files from new pg_multixact/offsets                  ok
Copying old pg_multixact/offsets to new server                ok
Deleting files from new pg_multixact/members                  ok
Copying old pg_multixact/members to new server                ok
Setting next multixact ID and offset for new cluster          ok
Resetting WAL archives                                        ok
Setting frozenxid and minmxid counters in new cluster         ok
Restoring global objects in the new cluster                   ok
Restoring database schemas in the new cluster
                                                              ok
Copying user relation files
                                                              ok
Setting next OID for new cluster                              ok
Sync data directory to disk                                   ok
Creating script to delete old cluster                         ok
Checking for extension updates                                ok

Upgrade Complete
----------------
Some statistics are not transferred by pg_upgrade.
Once you start the new server, consider running these two commands:
    /usr/local/bin/vacuumdb --all --analyze-in-stages --missing-stats-only
    /usr/local/bin/vacuumdb --all --analyze-only
Running this script will delete the old cluster's data files:
    ./delete_old_cluster.sh
 これで、リストアは完了です。。

5. サーバ起動

 「/etc/rc.conf」は、前のままにしているので、そのまま起動できるはずです。  サーバの起動。

service postgresql start
 下記のメッセージが出力されます。

start postgresql
 データベースのディレクトリが変わったので、コンフィグレーションファイルもいじらなければならない・・・。  が、データが入ったものの最適化されていない状態なので、最適化します。

su - postgres -c "/usr/local/bin/vacuumdb --all --analyze-in-stages"
 この結果。

vacuumdb: processing database "database_name": Generating minimal optimizer statistics (n target)
 という行が、データベースの数だけ出ます。  データをいくつか確認して、とりあえずバージョンアップ作業は一段落しました。

6. 設定

 あらためて、下記のファイルの設定をします(内容は、わたしの環境にあわせているものなのでご注意)。

/var/db/postgres/data18/pg_hba.conf
/var/db/postgres/data18/postgresql.conf
 ユーザ権限が、「postgres」にありますので。

sudo su postgres -c "vim -c ':set number' -c ':set mouse-=a' /var/db/postgres/data18/pg_hba.conf"
 デフォルトは、「PostgreSQL 16」のときと変わらないようです。  下記を環境に合わせて変更します。

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trust
 データベースをどこの誰からでも参照可能になっています。  これでは危険極まりないので、許可するアドレスを下記の要領で設定します。
TYPE localは UNIX のドメインソケット使用の接続を示し host は TCP/IP を使用した接続を示します。
DATABASEデータベース名を記述します
all を指定すると全データベースを指定することになります
USERロール名を指定します
CIDR-ADDRESS 標準のドット区切り10進表記でのIPアドレスとCIDRマスクの長さを指定します
METHODユーザ認証を行う方法を指定します
identident(RFC1413)による認証を行う
trust認証処理を行わず、スルーでパスさせる
reject接続を拒否する
password [password_file] パスワードファイルによる認証を行う
crypt PostgreSQL のシステムテーブル pg_shadow による認証を行う
kbr4/kbr5Kerberos V4/V5 による認証を行う

 「pg_hba.conf」は、上から順に見ていき、条件にマッチした行が見つかると、それが適用されます。
 次に。


sudo su postgres -c "vim -c ':set number' -c ':set mouse-=a' /var/db/postgres/data18/postgresql.conf"
 バージョン「9.6」までは、60、64 行を変更していました。  起動時のメッセージを読むと、少なくともポート番号は、デフォルトで「5432」が設定されているので、編集しなくてもいいかもしれません。

#listen_addresses = 'localhost'         # what IP address(es) to listen on;
                                        # comma-separated list of addresses;
                                        # defaults to 'localhost'; use '*' for all
                                        # (change requires restart)
#port = 5432                            # (change requires restart)
 ログを。

/var/log/postgresql/postgresql.log
 というファイルに出力させ、1日ずつローテーションさせることとしますと。

log_destination = 'syslog'
#log_destination = 'stderr'             # Valid values are combinations of
 を。

log_destination = 'syslog'
#log_destination = 'stderr'             # Valid values are combinations of
 に変えます。以下、矢印で・・・。

#logging_collector = off                # Enable capturing of stderr and csvlog
   ↓

logging_collector = on                  # Enable capturing of stderr and csvlog

#log_directory = 'log'                  # directory where log files are written,
   ↓

log_directory = '/var/log/postgresql'   # directory where log files are written,

#log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'        # log file name pattern,
   ↓

log_filename = 'postgresql.log'

#log_rotation_age = 1d                  # Automatic rotation of logfiles will
   ↓

log_rotation_age = 1d                   # Automatic rotation of logfiles will
 1日ごとにローテーションさせます。  ローテーションは、「newsyslog.conf」に書いてそちらで制御させようかとも思ったのですが、ファイルの所有権の問題がややこしいので。

#log_line_prefix = ''                   # special values:
   ↓

log_line_prefix = '%t '                 # special values:
 先頭に出力時刻をつけるようにします。  後ろにセパレータの空白か何かをいれないと後続のメッセージとくっついて出力されちゃいます。  ファイルが未作成の場合は、下記のコマンドで、初回出力用にフォルダとファイルを作成しまが。  既に運用中で、同じものがあるので、下記のコマンドは実行しません。

mkdir -pv /var/log/postgresql
touch /var/log/postgresql/postgresql.log
chown -R postgres:postgres /var/log/postgresql
 「PortgreSQL」を再起動して、ログを確認します。

service postgresql restart
 下記のメッセージが出力されます。

restart postgresql
 ログの中身を確認してみますと。

2026-11-14 22:31:26 JST LOG:  starting PostgreSQL 18.7 on x86_64-portbld-freebsd15.1, compiled by clang version 19.1.7, 64-bit
2026-11-14 22:31:26 JST LOG:  listening on IPv6 address "::1", port 5432
2026-11-14 22:31:26 JST LOG:  listening on IPv4 address "127.0.0.1", port 5432
2026-11-14 22:31:27 JST LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2026-11-14 22:31:27 JST LOG:  database system was shut down at 2026-11-14 22:31:26 JST
2026-11-14 22:31:27 JST LOG:  database system is ready to accept connections
 意図通りになりました。

7. 参考サイト

 本ページは、「Google」大先生を参考にさせていただきました。

audiobook.jp
U-NEXT
損保との違い
薬屋の独り言
アフィリエイトのアクセストレード
葬送のフリーレン Prime Video
【usus ウズウズ】
TikTok Shop 【リピート用プログラム】
5G CONNECT
サウンドハウス
健康サポート特集
ネットオークションの相場、統計、価格比較といえばオークファン
earthcar(アースカー)
JETBOY
それがだいじWi-Fi
ハイスピードプラン
Amazonギフトカード
ベルリッツ