这是我原来的错误:
$ psql
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"?
在 Rails 中......
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?
我搜索了一下,找到了大约十几个关于这个问题的答案——根本问题是,当我关闭计算机时,它/usr/local/var/postgres/postmaster.pid
并没有被删除——这意味着 postgres 没有正确关闭。我读到过,你不应该删除 pid(通常建议这样做),而是应该杀死它,即使有kill -9
——但同样,这只是一个临时解决方案,表明存在潜在错误。
编辑:我可以运行cat /usr/local/var/postgres/postmaster.pid
第一行并得到进程 ID。我最初犯了错误。
当我运行tail -r /usr/local/var/postgres/server.log | less
查看服务器日志(反向)时,我得到:
HINT: Is another postmaster (PID 1167) running in data directory "/usr/local/var/postgres"?
FATAL: lock file "postmaster.pid" already exists
HINT: Is another postmaster (PID 1167) running in data directory "/usr/local/var/postgres"?
FATAL: lock file "postmaster.pid" already exists
LOG: database system is shut down
LOG: shutting down
LOG: autovacuum launcher shutting down
LOG: received smart shutdown request
LOG: using stale statistics instead of current ones because stats collector is not responding
LOG: using stale statistics instead of current ones because stats collector is not responding
...
LOG: using stale statistics instead of current ones because stats collector is not responding
LOG: using stale statistics instead of current ones because stats collector is not responding
LOG: database system is ready to accept connections
LOG: autovacuum launcher started
LOG: MultiXact member wraparound protections are now enabled
LOG: database system was shut down at 2016-05-25 17:36:26 MDT
LOG: database system is shut down
LOG: shutting down
LOG: autovacuum launcher shutting down
LOG: received smart shutdown request
看起来数据库已正确关闭,但如果是这样,我相信文件postmaster.pid
会像我运行时一样被删除brew services stop postgres
。
答案1
该文件/usr/local/var/postgres/postmaster.pid
本身不是 PID,它是一个包含 PID 的文本文件。在本例中,PID 似乎是 1167。试试cat /usr/local/var/postgres/postmaster.pid
。该文件之所以存在,是因为出于某种原因,postgres 被不干净地关闭了。
如果这个 PID 1167 存在(ps -p $(cat /usr/local/var/postgres/postmaster.pid)
应该可以检查),那么您不应该删除该文件。如果在 postgres 运行时删除该文件,然后尝试重新启动 postgres,则很可能会对数据库造成严重损坏。
您应该停止 postgres 进程,无论何种方式,包括kill -9
—— 但只有在必要时才这样做。从正常命令开始,然后升级到kill
,只有当这不起作用时才升级到kill -9
。
一旦(或如果)文件中没有具有 PID 的进程,那么您可以删除该文件。
实际上,我认为 postgres 会检测到该进程不再存在并删除文件本身,但可以理解的是,我还没有对此进行太多实验!也许这取决于版本。