postgresql の移行

2023-09-14 に正式リリースされた PostgreSQL 16 が `apt upgrade` で降ってきた

$ sudo apt update && apt -y upgrade
##
The following NEW packages will be installed:
  postgresql-16 postgresql-client-16

ので、Mastodon ソロサーバーの丼くまを移行させることにした。

環境

$ cat /etc/os-release
PRETTY_NAME="Ubuntu 22.04.3 LTS"
$ uname -a
Linux kumarin 5.15.0-84-generic #93-Ubuntu SMP Tue Sep 5 17:16:10 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

もくじ

インストールされている postgres-16 の確認

postgresql-15 が稼働している状態で postgresql-16 が新たにインストールされると 16 はポート tcp/5433 で listen をはじめ、15 と共存状態になる。

$ netstat -an | grep -w LISTEN
tcp    0    0 127.0.0.1:5433    0.0.0.0:*    LISTEN    # postgresql-16
tcp    0    0 127.0.0.1:5432    0.0.0.0:*    LISTEN    # postgresql-15
$ sudo -u postgres -i
$ psql --port 5433

postgres=# select * From version();
PostgreSQL 16.0 (Ubuntu 16.0-1.pgdg22.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0, 64-bit

本番のポートは変えたくないので、事前に postgresql-16 のポートを 5432 に変更しておき、移行実施時に 15 → 16 とサービスを切り替えることにする。

下準備

postgresql のユーザの作成

tcp/5433 で listen 中の postgresql-16 に `createuser` を発行し、mastodon ユーザを作成しておく。

$ sudo -u postgres -i
postgres:~$ createuser --port 5433 --createdb mastodon
postgres:~$ exit

チューニング (2023.9.26 追記)

チューニングの整定または引き継ぎは、ここで行っておくべきだろう。
(立ち上げ以来さぼってたので、移行の後日設定した)

$ sudo -u postgres -i
postgres:~$ psql --port 5433

postgres=# ALTER SYSTEM SET max_connections = '40';
ALTER SYSTEM SET shared_buffers = '64MB';
...
postgres=# quit

postgres:~$ exit

チューニングは PGTune で生成してもらうと楽。

ポートの変更

ポートを本来の 5432 に変更するため、postgresql-16 サービスをとめて自動起動を無効にする。

$ sudo systemctl stop postgresql@16-main.service
$ sudo systemctl disable postgresql@16-main.service

postgresql-16 の postgresql.conf 編集し、`port` を 5433 から 5432 に変更。
次回起動時は tcp/5432 で listen するようになる。

$ sudo vim /etc/postgresql/16/main/postgresql.conf
#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------

# - Connection Settings -
port = 5432

移行の実施

Mastodon のサービスをとめる

$ sudo systemctl stop mastodon-web mastodon-sidekiq mastodon-streaming

既存データベースを待避する

$ sudo -u mastodon -i
mastodon:~$ pg_dump -Fc mastodon_production -f ~/donbak/pg_backup.dump
mastodon:~$ exit

postgresql サービスの切り替え

15 を停止 & サービス無効。
16 を始動 & サービス有効。

$ sudo systemctl stop postgresql@15-main.service
$ sudo systemctl disable postgresql@15-main.service
$ sudo systemctl start postgresql@16-main.service
$ sudo systemctl enable postgresql@16-main.service

既存データベースの復元

$ sudo -u mastodon -i
mastodon:~$ createdb mastodon_production
mastodon:~$ pg_restore --dbname mastodon_production ~/donbak/pg_backup.dump
mastodon:~$ exit

ソロ鯖であんまりアグレッシブに投稿していないこともあってか2分くらいで終わった。
一人前のサーバーならもっともっとかかると思う。

Mastodon のサービスを始動

$ sudo systemctl start mastodon-web mastodon-sidekiq mastodon-streaming

サービスが正しく動いていることと、管理ダッシュボードの Software で PostgreSQL の版が 16 になっていることを確認して完了。

あとかたづけ

しばらく期間をあけて安全を確認してからがいいかも。

$ sudo apt purge --auto-remove postgresql-15 postgresql-client-15

`apt purge` により 1.7G あった postgresql-15 のデータディレクトリ (/var/lib/postgresql/15) が削除された。
ちなみに、16 のほうは 2.3G になってた。なんで。

links

PostgreSQL: PostgreSQL 16 Released!
https://www.postgresql.org/about/news/postgresql-16-released-2715/

PostgreSQL: Documentation: 16: E.1. Release 16
https://www.postgresql.org/docs/16/release-16.html


トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2023-09-26 (火) 01:29:41