私はよく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ファイルでのプロセス管理が破綻して起動できなかった?