LoginSignup
2
3

【2024/03時点】M1 MacでのPostgreSQLインストール手順

Posted at

はじめに

M1 MacでのPostgreSQLインストール手順をメモしておきます。

ただし、色々詰まって試しながらやったので、完全な手順の再現はできていないと思います。ご了承ください。

環境

  • M1 Mac(Sonoma 14.3.1)
  • postgres (PostgreSQL) 14.11 (Homebrew)

インストール手順

※ Homebrewがインストールされている前提です。

おそらく以下の手順でやればうまくいくはず。

1. PostgreSQL用のドライバのインストール

$ go get github.com/lib/pq

2. PostgreSQLのインストール

@14をつけないとWarningが出る

$ brew install postgresql@14

インストール後に$ initdb /usr/local/var/postgres -E utf8を実行して文字コードをUTF-8にするよう書かれている記事もありますが、現在では最初からUTF-8に設定されているため不要のようです。

バージョンの確認

$ postgres --version

3. PostgreSQLサーバの起動

$ brew services start postgresql@14

4.サービスの起動確認

$ brew services list
Name          Status  User File
postgresql@14 started wsigma21  ~/Library/LaunchAgents/homebrew.mxcl.postgresql@14.plist

5.データベース一覧の取得

$ psql -l

6.PostgreSQLサーバの停止

$ brew services stop postgresql@14

参考

エラー

インストール時に起きた複数のエラーについて、私が試したり調べたりしたことがどなたかの役に立てばと思い、記録しておきます。

結論を言うと、色々試したのですが私の環境での原因はよくわかっていません。
最終的にアンインストールしてMacを再起動してインストールしなおしたらうまく行きました。

その1「Warning: The post-install step did not complete successfully」

インストール時に以下のWarningが出ました。

Warning: The post-install step did not complete successfully
You can try again using:
  brew postinstall postgresql@14

指示通り「$ brew postinstall postgresql@14」を実行しても同じWarningが出ます。
一旦無視して$ brew services start postgresql@14を実行するとSuccessfullyになるのですが、サービスの状態はerrorでした。

$ brew services list
Name          Status     User          File
postgresql@11 error  512 wsigma21 ~/Library/LaunchAgents/homebrew.mxcl.postgresql@14.plist

以下の記事を参考に/private/tmpのパーミッションを確認したのですが、元からrwxrwxrwtに設定されていたので、パーミッションは関係なさそうでした。。

その2「Bootstrap failed: 5: Input/output error」

起動時にこちらのエラーが出たこともありました。

Warning: Use postgresql@14 instead of deprecated postgresql
Bootstrap failed: 5: Input/output error
Try re-running the command as root for richer errors.
Error: Failure while executing; `/bin/launchctl bootstrap gui/501 /Users/wsigma21/Library/LaunchAgents/homebrew.mxcl.postgresql@14.plist` exited with 5.

以下の記事を参考にpostmaster.pidの削除を試みましたが、そもそも/opt/homebrew/var/postgres@14が空でした(この件については次項で記述)

その3「psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory」

起動時にこちらのエラーが出たこともありました。

一度Postgresを停止・再起動すればうまくいくという記事もありましたが、私の環境では変化ありませんでした。

次に、以下の記事を参考に、ログを確認してみました。

$ cat /opt/homebrew/var/log/postgresql@14.log 
postgres: could not access the server configuration file "/opt/homebrew/var/postgresql@14/postgresql.conf": No such file or directory

そもそもpostgresql.confがないことがエラーの原因な気がしてきました。

確認すると/opt/homebrew/var/postgresql@14の下にやはり何もありません。

$ ls -la /opt/homebrew/var/postgresql@14
total 0

最初にインストールを試みたときのコマンドがM1用ではなく色々試したため、正しくインストールされていなかったのでしょう。

アンインストール

インストール失敗が原因なら、アンインストールして再インストールすればいいはずです。

  • (起動している場合は)サーバの起動停止
$ brew services stop postgresql@14
  • 不要なディレクトリの削除
$ rm -rf /opt/homebrew/var/postgresql@14
  • アンインストール
$ brew uninstall postgresql@14
  • Macの再起動(一応)

再起動後、序盤に記述したインストール手順に従って再インストールしたところ、正常に動作するようになりました

終わりに

Intel プロセッサでは/user/var/postgresにインストールされますが、Appleシリコンでは/opt/homebrew/var/postgresql@14にインストールされます。

そのことを最初に確認し、その直下を見に行けば最短でインストール時の不具合と見抜けたと思います。

無闇にエラーメッセージでググり続ける悪癖を直したいです。

2
3
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
2
3