LoginSignup
9
10

More than 1 year has passed since last update.

PostgreSQLに急に接続できなくなった時の対処法

Last updated at Posted at 2019-10-16

問題

PythonからPostgreSQLにいつも通りにアクセスして作業しようと思ったらエラーで動かず。

OperationalError: 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?

次にターミナルでPostgreSQLの中に入ろうとしたら今度は以下のようなエラーが。。。

~ $ psql hoge
psql: 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"?

どうやらpostmaster.pidというファイルが原因らしいです。
postmaster.pidがどう悪さをしているのかはよくわからないです。
スクリーンショット 2019-10-16 22.57.05.png

解決法

上記のpostmaster.pidを取り除いてやります。
ターミナルで以下のコードを打って実行します。

$ rm -f /usr/local/var/postgres/postmaster.pid
$ brew services start postgres

PostgreSQLを再起動したら正常にstartできるようになりました。

ちなみにアラートを無視して強制的にファイルを削除するオプションの-fを付けずにremoveすると、errorもwarningも出ないので問題なく消せたように思えましたが、lsでディレクトリを見ると残っていました。postmaster.pidが無い状態でPostgreSQLを再起動する必要があり、どうしようかと。試しに-fを付けるとちゃんと消えました。

rm /usr/local/var/postgres/postmaster.pid
9
10
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
9
10