1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

macのPostgreSQLをバージョン変更してみた(Homebrew)

Last updated at Posted at 2025-04-06

モチベーション

現在Rails環境構築が不完全でアプリ開発できていない。 Railsバージョン更新を行ったことが一因として挙げられる。 関連アプリのバージョンを更新する一環でPostgreSQLのバージョン更新を記録する。

参考記事[1]をベースに作成しました!とても分かりやすいです!

実行手順

1. 現在のpostgreSQLのバージョン確認

ターミナル
$ psql --version
psql (PostgreSQL) 14.17 (Homebrew)

2. Homebrewから取得できるpostgreSQLのバージョン確認

チェックがついているものがインストールしてあるもの。 以前アップデートのためインストールした@17があります。 @14を削除して@17のみを残します。
ターミナル
$ brew search postgresql
==> Formulae
postgresql-hll       postgresql@12        postgresql@14       postgresql@16        qt-postgresql
postgresql@11        postgresql@13        postgresql@15        postgresql@17       postgrest

3. 現在のpostgreSQL@14のアンインストール

ターミナル
$ brew uninstall postgresql@14
Uninstalling /opt/homebrew/Cellar/postgresql@14/14.17_1... (3,330 files, 45.9MB)

4. アンインストール確認

ターミナル
$ brew search postgresql
==> Formulae
postgresql-hll       postgresql@12        postgresql@14        postgresql@16        qt-postgresql
postgresql@11        postgresql@13        postgresql@15        postgresql@17       postgrest

5. バージョン確認

@17があるはずなのにnot found。次で再リンクを行います。
ターミナル
$ psql --version
zsh: command not found: psql

6. リンク解除

ターミナル
$ brew unlink postgresql@17
Unlinking /opt/homebrew/Cellar/postgresql@17/17.4_1... 0 symlinks removed.

7. 再リンク

ターミナル
$ brew link postgresql@17
Linking /opt/homebrew/Cellar/postgresql@17/17.4_1... 802 symlinks created.

If you need to have this software first in your PATH instead consider running:
  echo 'export PATH="/opt/homebrew/opt/postgresql@17/bin:$PATH"' >> ~/.zshrc

上記のように"echo 'export PATH=~"をやるように!と言われます。これは「zshのような環境変数を格納しておくファイルにパスを保存しておいてね。保存しておかないとDB使う時にパスがわからなくてエラー表示するよ。」の意味みたいです[2]。
以下ではパスがzshにそもそもあるか確認、保存して再確認します。

8. 環境変数格納パスの確認・保存・再確認

8-1. 環境変数格納パス一覧表示

以下を実行すると、指定されたパス("/opt/homebrew/opt/~")がないことが分かりました。

ターミナル
$ echo $PATH | sed -e 's/:/\n/g'
・・・(省略)・・・
/opt/homebrew/bin
/opt/homebrew/sbin
/usr/local/bin
・・・(省略)・・・
<h3>8-2. PATHの設定</h3>
指定コマンドをそのまま入力[3]。特に出力はありません。

```ruby:ターミナル
$ echo 'export PATH="/opt/homebrew/opt/postgresql@17/bin:$PATH"' >> ~/.zshrc

8-3. PATHの保存

以下のコマンドでPATHを保存[3]。これも特に出力はありません。
ターミナル
$ source ~/.zshrc

8-4. 保存したPATHの確認

以下のコマンドでPATHを保存[3]。8-2で保存したパスが表示されれば完了!
ターミナル
$ which psql
/opt/homebrew/opt/postgresql@17/bin/psql

9. バージョン確認

リンクできました。
ターミナル
$ psql --version
psql (PostgreSQL) 17.4 (Homebrew)

まとめ

macでHomebrewのpostgreSQLバージョン更新を行いました。 環境構築の進展があれば記事とともに報告します。 この記事が誰かの役に立てられれば幸いです。 閲覧ありがとうございました。

参考資料

[1]@yuppymam.(2023).【Mac】Homebrew を使って PostgreSQL を動かす.https://qiita.com/yuppymam/items/fad4eec0c5b7d6c86517

[2]@ishidao(石田尾 豪).(2022).開発者であれば最初はきっと沼る、PATHと.zshrc周りを調べまくったので備忘録.https://qiita.com/ishidao/items/769b2e9acfc2ce4542b6

[3]Curry (id:dev-0).(2025).PostgreSQLをmacにHomebrewでインストールする手順.https://mkdir-cd.hatenablog.com/entry/2025/01/06/232458

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?