- 1. 概要
- 2. 検討
- 3. バックアップ・サーバ停止
- 4. postgreSQL 16 インストール
- 5. サーバ起動
- 6. リストア
- 7. 設定
1. 概要
いつもは、細かくチェックしていないのですが。
ふと、「daily security run output」をえらいことになっとる。
Database fetched: 2024-11-24T06:30+09:00
db5-5.3.28_9: Tag: expiration_date Value: 2022-06-30
postgresql12-client-12.22: Tag: expiration_date Value: 2024-12-31
postgresql12-server-12.22: Tag: expiration_date Value: 2024-12-31
db5-5.3.28_9: Tag: deprecated Value: EOLd, potential security issues, maybe use db18 instead
dialog4ports-0.1.6_2: Tag: deprecated Value: Consider using ports-mgmt/portconfig which is a dropin replacement
postgresql12-client-12.22: Tag: deprecated Value: PostgreSQL-12 is end-of-life
postgresql12-server-12.22: Tag: deprecated Value: PostgreSQL-12 is end-of-life
「postgreSQL 12」は、2024年12月31日までの命ですってよ。
ちゃんと目を通しておかないとダメですね。
2. 調査
「ports」の状態を見てみると
$ ls -d /usr/ports/databases/postgresql*-server
/usr/ports/databases/postgresql12-server /usr/ports/databases/postgresql15-server
/usr/ports/databases/postgresql13-server /usr/ports/databases/postgresql16-server
/usr/ports/databases/postgresql14-server /usr/ports/databases/postgresql17-server
これも、気づいておらなんだが、「postgreSQL 17」まで、リリースされているのね。
まぁ、最新は、とりあえず、避けるとして・・・。
少し上げるならば、「postgreSQL 14」なのですが。
いずれまた、あげることになるので、今回は、「postgreSQL 16」へ移行することにします。
3. バックアップ・サーバ停止
以降の、全項目、「root」ユーザ権限で。
現状のデータをバックアップ(そこまで大きくないので、テキストファイルに保存可能なのだ)。
sudo su postgres -c "pg_dumpall --column-inserts > pg_dumpall.sql"
サーバ停止。
service postgresql stop
データベース全体をバックアップ。
cd /var/db/postgres
cp -R data12 data12-20241126-backup
バックアップのファイル名は、ここではその日の名前にしました。
4. postgreSQL 16 インストール
コンフィグレーションを先に設定しておきます。
cd /usr/ports/databases/postgresql16-server/
make config
オプションは、デフォルトのままでよいかと思います。
「postgreSQL 9.6」を「postgreSQL 12」へアップグレードしたときに。
「pkg set」やら「portupgrade」で結局ダメだったので、今回はすなおに、「server」「slient」をアンインストールしておきます。
cd /usr/ports/databases/postgresql12-client/
make deinstall clean
cd /usr/ports/databases/postgresql12-server/
make deinstall clean
その上で。
cd /usr/ports/databases/postgresql16-server
make
make install
「postgresql16-server」のインストールには、「llvm15」が必要なようです。
「postgresql12-server」のときは、「llvm10」が必要で。
「llvm10」のインストールには、相当な時間がかかりますので、「postgreSQL」サーバを停止する時間を短くしたい場合は、サーバを停止する前に「llvm10」を先にインストールしておいたのですが。
「llvm15」は、たいてい既にインストールされているかと思います。
5. サーバ起動
データベースを初期化します。
service postgresql initdb
下記のメッセージが、表示されます。
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 this locale configuration:
provider: libc
LC_COLLATE: C
LC_CTYPE: C.UTF-8
LC_MESSAGES: C.UTF-8
LC_MONETARY: C.UTF-8
LC_NUMERIC: C.UTF-8
LC_TIME: C.UTF-8
The default text search configuration will be set to "english".
Data page checksums are disabled.
creating directory /var/db/postgres/data16 ... 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/data16 -l logfile start
データベースは。
/var/db/postgres/data16/
に作成されます。
「/etc/rc.conf」は、前のままにしているので、そのまま起動できるはずです。
サーバの起動。
service postgresql start
下記のメッセージが出力されます。
2024-11-26 14:58:49.905 JST [25128] LOG: ending log output to stderr
2024-11-26 14:58:49.905 JST [25128] HINT: Future log output will go to log destination "syslog".
データベースのディレクトリが変わったので、コンフィグレーションファイルもいじらなければならない・・・。
まぁ、とりあえずそれは、後回しで、次項のリストアを先にいたします。
6. リストア
バックアップをとっておいた、ディレクトリへ移動して・・・。
sudo su postgres -c "psql --quiet --username=postgres --file pg_dumpall.sql"
データをいくつか確認して、とりあえずバージョンアップ作業は一段落しました。
7. 設定
ディレクトリが変わりましたので、「postgreSQL12」のときに設定していた、コンフィグレーションファイルは、ご破算になっています。
あらためて、下記のファイルの設定をします(内容は、わたしの環境にあわせているものなのでご注意)。
/var/db/postgres/data16/pg_hba.conf
/var/db/postgres/data16/postgresql.conf
ユーザ権限が、「postgres」にありますので。
sudo su postgres -c "vim -c ':set number' -c ':set mouse-=a' /var/db/postgres/data16/pg_hba.conf"
下記を環境に合わせて変更します。
# 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 | ユーザ認証を行う方法を指定します
ident | ident(RFC1413)による認証を行う |
trust | 認証処理を行わず、スルーでパスさせる |
reject | 接続を拒否する |
password [password_file] |
パスワードファイルによる認証を行う |
crypt |
PostgreSQL のシステムテーブル pg_shadow による認証を行う |
kbr4/kbr5 | Kerberos V4/V5 による認証を行う |
|
「pg_hba.conf」は、上から順に見ていき、条件にマッチした行が見つかると、それが適用されます。
次に。
sudo su postgres -c "vim -c ':set number' -c ':set mouse-=a' /var/db/postgres/data16/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
下記のメッセージが出力されます。
2024-11-26 15:30:40 JST LOG: redirecting log output to logging collector process
2024-11-26 15:30:40 JST HINT: Future log output will appear in directory "/var/log/postgresql".
ログの中身を確認してみますと。
2024-11-26 15:30:40 JST LOG: starting PostgreSQL 16.6 on amd64-portbld-freebsd13.4, compiled by clang version 15.0.7 (https://git.FreeBSD.org/ports.git 4893564820776531ba152dd47401088d5f6f35a3), 64-bit
2024-11-26 15:30:40 JST LOG: listening on IPv6 address "::1", port 5432
2024-11-26 15:30:40 JST LOG: listening on IPv4 address "127.0.0.1", port 5432
2024-11-26 15:30:40 JST LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
2024-11-26 15:30:40 JST LOG: database system was shut down at 2024-11-26 15:30:40 JST
2024-11-26 15:30:40 JST LOG: database system is ready to accept connections
意図通りになりました。
「ports」の更新状況を見てみましたが、2024年11月26日時点では、更新は、発生していないようです。
|