happiness (^_^) (^_^) (^_^) (^_^) (^_^) (^_^) (^_^) (^_^) (^_^) (^_^) (^_^) (^_^) happiness

【PostgreSQL】PG::ConnectionBad:の対処brewでインストールした場合

エラー内容

     PG::ConnectionBad:
       could not connect to server: Connection refused
        Is the server running on host "localhost" (::1) and accepting
        TCP/IP connections on port 5432?
       could not connect to server: Connection refused
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 5432?

発生した

macを再起動してrspec実行しようとしたら、出た

原因

古いpidファイルが残っているから、processの識別番号なので、 古いのが残っている→まだプロセスが生きているとみなされるかなんかで、だめになる。

自動で古いのを消せないのが原因?わからんです

対処方法

pidファイルを消す

brewpsqlをインストしている人は、/opt/brew/var/postgresを見に行ったほうがいいかも

/user/local/var/postgres配下にpidファイルあるよという記事が多いが各環境によるため

brew services start postgresql

rm /opt/brew/var/postgres/postmaster.pid

Success!

その他の対処方法

brew service list

# エラーになってたら
brew service stop postgresql

# 必要に応じて
pg_ctl -D /opt/brew/var/postgres/ start

蛇足

macをスリープモードにしておいて、バッテリー切れで電源切れた時に発生することがあるので注意

その他

 Is the server running locally and accepting
    connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

ってエラーが出た場合も同様

brew upgradeしてしまった場合に、postgresqlが巻き込まれてこのエラーが出ることもある。

下記が対処法。

※pathには注意。

zenn.dev