homebrew でインストールしたPostgreSQL 15 から 16 へのアップデートをする

homebrew でインストールしたPostgreSQL 15 から 16 へのアップデートをする

homebrew ではpostgresql がバージョンごとに複数インストール可能です。 しかしほぼ下位互換性が保たれているので、ガンガンアップデートして問題ないと考えています。

PostgreSQL のメンテナンスをされている方々には感謝の気持ちを持っています。

毎回忘れて時間を使うので自分への備忘録も兼ねて書いておきます。

先にPostgreSQL のプロセスはぶっ殺しておいてください homebrew でインストールしたPostgreSQL の起動方法は2種類あります 起動方法は brew info コマンドで確認が可能です (この記事を書いている段階でpostgresql@15はアンインストールしてしまったので16のコマンド)

$ brew info postgresql@16
==> postgresql@16: stable 16.3 (bottled) [keg-only]
Object-relational database system
https://www.postgresql.org/
Installed
/opt/homebrew/Cellar/postgresql@16/16.3 (3,801 files, 68.4MB) *
  Poured from bottle using the formulae.brew.sh API on 2024-08-08 at 14:17:04
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/p/[email protected]
License: PostgreSQL
==> Dependencies
Build: pkg-config ✔
Required: gettext ✔, icu4c ✔, krb5 ✔, lz4 ✔, openssl@3 ✔, readline ✔, zstd ✔
==> Caveats
This formula has created a default database cluster with:
  initdb --locale=C -E UTF-8 /opt/homebrew/var/postgresql@16
For more details, read:
  https://www.postgresql.org/docs/16/app-initdb.html

postgresql@16 is keg-only, which means it was not symlinked into /opt/homebrew,
because this is an alternate version of another formula.

If you need to have postgresql@16 first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/postgresql@16/bin:$PATH"' >> ~/.zshrc

For compilers to find postgresql@16 you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/postgresql@16/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/postgresql@16/include"

For pkg-config to find postgresql@16 you may need to set:
  export PKG_CONFIG_PATH="/opt/homebrew/opt/postgresql@16/lib/pkgconfig"

To start postgresql@16 now and restart at login:
  brew services start postgresql@16  #<= これ
Or, if you don't want/need a background service you can just run:
  LC_ALL="C" /opt/homebrew/opt/postgresql@16/bin/postgres -D /opt/homebrew/var/postgresql@16 #<= これ
==> Analytics
install: 5,862 (30 days), 18,952 (90 days), 65,673 (365 days)
install-on-request: 5,776 (30 days), 18,586 (90 days), 63,672 (365 days)
build-error: 1 (30 days)

service で上げている場合

$ brew services stop postgresql@16

コマンドで手動で上げている場合

$ ps aux|grep postgres
katsusuke        26761   1.7  0.1 410684096  10832 s001  S     1:53PM   0:00.01 q _ inline-shell-completion --buffer ps aux|grep postgres
katsusuke         9647   0.0  0.0 411092656   2160   ??  SNs  金10AM   0:00.28 postgres: logical replication launcher   
katsusuke         9646   0.0  0.0 411116208   2608   ??  SNs  金10AM   0:12.16 postgres: autovacuum launcher   
katsusuke         9645   0.0  0.0 411088560    752   ??  SNs  金10AM   0:06.58 postgres: walwriter   
katsusuke         9643   0.0  0.0 410956464   1008   ??  SNs  金10AM   0:00.72 postgres: background writer   
katsusuke         9642   0.0  0.0 411378352   1504   ??  SNs  金10AM   0:06.19 postgres: checkpointer   
katsusuke         9632   0.0  0.0 410969072   3232   ??  SN   金10AM   0:54.32 /opt/homebrew/opt/postgresql@16/bin/postgres -D /opt/homebrew/var/postgresql@16 #<= これ
katsusuke        26763   0.0  0.0 410733264   1504 s001  S+    1:53PM   0:00.00 grep postgres
# kill でmaster プロセスを殺す
kill 9632

DBのアップデート

brew install postgresql@16
# brew でインストールした時に自動生成されるDBのフォルダーは後で作り直すので一旦削除
rm -rf /opt/homebrew/var/postgresql@16
/opt/homebrew/opt/postgresql@16/bin/initdb --locale=C -E UTF-8 /opt/homebrew/var/postgresql@16
/opt/homebrew/opt/postgresql@16/bin/pg_upgrade -b /opt/homebrew/opt/postgresql@15/bin -d /opt/homebrew/var/postgresql@15 -D /opt/homebrew/var/postgresql@16

PostgreSQL@15 をアンインストールします

brew uninstall postgresql@15

/opt/homebrew/bin/psql などのシンボリックリンクも更新します

brew link postgresql@16

起動して、psql で接続して正常にデータが移行できたか確認します

LC_ALL="C" /opt/homebrew/opt/postgresql@16/bin/postgres -D /opt/homebrew/var/postgresql@16 &

移行ができてそうならデータも消しちゃいましょう

rm -rf /opt/homebrew/var/postgresql@15
この記事をシェア

弊社では、一緒に会社を面白くしてくれる仲間を募集しています。
お気軽にお問い合わせください!