我和其他一些人一样,当我在我的项目中运行rake db:migrate甚至尝试大多数数据库任务时,都会出现这个错误,我的Ruby on Rails 3.2应用程序也是如此。
PGError(无法连接到服务器:没有此文件或目录。 服务器是否正在本地运行并接受"/ tmp / .s.PGSQL.5432" Unix域套接字上的连接?
很久以前,我使用Homebrew安装了PostgreSQL,最近尝试安装MongoDB后,我的PostgreSQL安装就再也没有正常工作过。 我正在运行OS X v10.6 Snow Leopard。
出了什么问题?我如何更好地理解如何在Mac上设置PostgreSQL?
到目前为止(我想)这告诉我PostgreSQL没有运行(?)。
ps -aef|grep postgres (ruby-1.9.2-p320@jct-ana) (develop) ✗
501 17604 11329 0 0:00.00 ttys001 0:00.00 grep postgres
这告诉我PostgreSQL正在运行吗?
✪ launchctl load -w /usr/local/Cellar/postgresql/9.1.4/homebrew.mxcl.postgresql.plist (ruby-1.9.2-p136)
homebrew.mxcl.postgresql: Already loaded
我该如何修复这个问题?我有什么遗漏的吗?PS: ~/Library/LaunchAgents 包含两个PostgreSQL .plist文件。不确定它是否相关。
org.postgresql.postgres.plist
homebrew.mxcl.postgresql.plist
我尝试了下面的命令并得到以下结果。
$ psql -p 5432 -h localhost
psql: 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?
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?
据我所知,这是因为OS X安装了自己的PostgreSQL版本,而Homebrew在不同的位置安装了另一个版本,而PostgreSQL命令正在查找/tmp/目录。你需要在Stack Overflow上进一步搜索,但基本上你需要创建PostgreSQL的符号链接,这样任何查找tmp路径的内容都可以找到真实路径。
这是我找到的一个链接,里面提供了更多尝试的方法,特别是按照上述方法创建符号链接:Mac OSX Lion Postgres does not accept connections on /tmp/.s.PGSQL.5432。我仍然希望有人能够给出一个关于在OS X上安装PostgreSQL的概念和为什么会这么困难的合理解释。
最新的见解可帮助故障排除:
$ which psql // This tells you which PostgreSQL you are using when you run $ psql.
然后运行:
$ echo $PATH
需要注意的关键事项是:
确保要运行的PostgreSQL副本的路径条目在OS X系统的PostgreSQL路径之前。
这是一个核心要求,决定哪个PostgreSQL被运行,也是我听说导致大多数问题的原因。