6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Mac homebrew で service 起動した postgresql に接続できないときは、pidファイルが怪しい

Last updated at Posted at 2017-11-10

私はよくMacのローカルでRails > Postgresqlを動かすのですが、以下のようなエラーに悩まされたりすることがたまにあります。

# <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" (fe80::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?

restartしてもだめ

$ brew services restart postgresql
$ ps aux | grep postgresql
soeda            13514   0.0  0.0  2443044    808 s001  S+    3:04PM   0:00.00 grep postgresql

プロセスisない

結局postgresqlをuninstallしてinstall

$ brew uninstall postgresql
$ rm -rf /usr/local/var/postgres
$ brew install postgresql

これで直りますけど、DB再作成などしなきゃでしんどい。

PIDファイルっぽい

$ brew services start postgresql
$ ps aux | grep postgresql
soeda            13514   0.0  0.0  2443044    808 s001  S+    3:04PM   0:00.00 grep postgresql

実際のプロセス確認しようにも、そもそも起動できてないですね

$ cat /usr/local/var/postgres/postmaster.pid 
13457
/usr/local/var/postgres
1510293800
5432
/tmp
localhost
  5432001     65536

PIDファイルはあるな...
(通常通りstopしてもPIDファイルはあるみたい)

$ brew services stop postgresql
$ rm /usr/local/var/postgres/postmaster.pid

PID消してみた

$ brew services start postgresql
$ ps aux | grep postgresql
soeda            13514   0.0  0.0  2443044    808 s001  S+    3:04PM   0:00.00 grep postgresql
soeda            13457   0.0  0.1  2605700  13796   ??  S     3:03PM   0:00.03 /usr/local/opt/postgresql/bin/postgres -D /usr/local/var/postgres

動いた!

考察

brewが管理できないところでpostgresqlプロセスが落ちてしまって、PIDファイルでのプロセス管理が破綻して起動できなかった?

6
5
1

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
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?