💻

【PostgresSQL】サーバが起動できない【PG::ConnectionBad】【/tmp/.s.PGSQL.5432】

2022/01/16に公開

エラー発生

前に作ったアプリをrails sし起動しようとすると

PG::ConnectionBad: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

この様なエラーになった方多いと思います(私も前に何度かハマっています)。
今回はこれを解消していきます。

原因特定 -lock file "postmaster.pid" already exists

まず上記エラーをDeepLで翻訳

PG::ConnectionBad: サーバーに接続できませんでした。No such file or directory サーバーはローカルで動作しており、Unix ドメインソケット "/tmp/.s.PGSQL.5432" で接続を受け付けているか?

とりあえずサーバーに接続ができないらしい。

次はエラー文を検索
とするとある記事で

postgres -D /usr/local/var/postgres

をターミナルで実行すると通常であればポスグレの起動ができるが、
問題があるとログファイルにアクセスしデバックが出来るとのこと。

実際に実行すると、

kanta@fukazawakantanoMacBook-Pro kantafkzw-blog-app % postgres -D /usr/local/var/postgres
2022-01-16 12:33:24.358 JST [9335] FATAL:  lock file "postmaster.pid" already exists 
2022-01-16 12:33:24.358 JST [9335] HINT:  Is another postmaster (PID 5957) running in data directory "/usr/local/var/postgres"? 
  • "postmaster.pid"が既に存在している。
  • データディレクトリ "/usr/local/var/postgres" で別の postmaster (PID 5957) が動作 しているか?

トライ

"postmaster.pid"が既に存在している。
データディレクトリ "/usr/local/var/postgres" で別の postmaster (PID 5957) が動作 しているか?

どうやらgoogleで確認すると以前起動した別サーバーが邪魔している様子。

そのためlock file "postmaster.pid"を削除が必要。
いろいろなアプローチがある様だがターミナルで以下のコマンドを実行

rm /usr/local/var/postgres/postmaster.pid

これでも消えない場合は(おそらくファイルが特定できていない)、

rm -f data/postmaster.pid

これでファイルの特定をして削除が出来る

これでrails sで起動できました。

Discussion