1
0

More than 1 year has passed since last update.

[対処] PG::ConnectionBad: could not connect to server on socket

Posted at

環境

  • MacOS

現象

rails db:migrate を実行しようとしたら以下のエラーに遭遇。

PG::ConnectionBad: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory
Is the server running locally and accepting connections on that socket

PG::ConnectionBad: connection to server on socket で調べるとpostgresql関連の問題らしい。
以下のコマンドを実行してpostgresqlの状態を確認。

$ brew services list                         
Name       Status  User Plist    
postgresql error   user  /path/to/postgresql/homebrew.mxcl.postgresql.plist

ひとまず再起動を試す。

brew services restart postgresql
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

成功したっぽいが、 brew services list を再実行すると相変わらずpostgresqlはerrorのまま。
調べながら以下のコマンドを試す。

$ postgres -D /usr/local/var/postgres
The data directory was initialized by PostgreSQL version 13, which is not compatible with this version 14.2.

対処

バージョン違いっぽい。ので、再度エラーメッセージで検索して出てきた以下の記事を参考に PostgreSQL version 13 をインストール。
https://zenn.dev/koniman/articles/a1acab134a43aa

 $ brew install postgresql@13
 $ brew services stop postgresql
 $ brew services start postgresql@13

確認。

$ brew services list                         
Name          Status  User Plist    
postgresql    none
postgresql@13 started user /path/to/homebrew.mxcl.postgresql@13.plist

この後無事に rails db:migrate を実行できた。

備考

先週 brew update を実行するつもりが誤って brew upgrade を実行してしまったので多分postgresqlも最新になったっぽい。

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