这是我原来的错误:
$ 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?
我search了一下,发现了大约十几个答案 – 这个问题的关键是,当我closures我的电脑/usr/local/var/postgres/postmaster.pid不会被删除 – 意味着postgres没有正常关机。 我已经读过,你不应该删除pid(正如通常所推荐的),而是杀死它,即使kill -9 – 但是这又是一个临时解决scheme,并且暗示了一个潜在的错误。
编辑:我可以运行cat /usr/local/var/postgres/postmaster.pid第一行给我的进程ID。 我原本有这个错误。
当我运行tail -r /usr/local/var/postgres/server.log | less 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
它看起来像数据库正确closures,但如果是这样的话,我相信postmaster.pid文件将被删除,就像当我运行如brew services stop postgres 。
文件/usr/local/var/postgres/postmaster.pid本身不是一个PID,它是一个包含PID的文本文件。 在这种情况下,它似乎是1167.试试cat /usr/local/var/postgres/postmaster.pid 。 该文件是因为某些原因postgres被closures不干净。
如果这个PID 1167存在( ps -p $(cat /usr/local/var/postgres/postmaster.pid)应该检查),那么你不应该删除这个文件。 如果在运行postgres时删除文件,然后尝试重新启动postgres,则很可能会对数据库造成严重损坏。
你应该停止postgres进程,通过任何方式直到并包括kill -9 – 但是这只在必要的时候才能使用 。 从正常的命令开始,升级kill ,只有这样做不能kill -9 。
一旦(或如果)你没有在文件中的PID进程,那么你可以删除该文件。
实际上,我认为postgres会发现这个过程不再存在,并删除文件本身,但可以理解的是,我没有尝试太多这个! 也许这取决于版本。