如果在Mac homebrew上启动的service无法连接到postgresql时,可能是pid文件有问题
我经常在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?
重启也没用。
$ 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
没有进程
最终,卸载了postgresql并重新安装。
$ brew uninstall postgresql
$ rm -rf /usr/local/var/postgres
$ brew install postgresql
这样就修好了,但是重新建立数据库之类的工作很辛苦。
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文件还在…
(停止时通常会删除PID文件,但是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
动了!
考察 chá)
在brew无法管理的地方,postgresql进程崩溃了,导致使用PID文件进行的进程管理出现故障,所以无法启动?