无法连接到本地PostgreSQL

And*_*vey 124 postgresql macos ruby-on-rails

我设法让我的本地开发环境变得笨拙.

我所有的本地Rails应用程序现在都给出错误:

PGError
could not connect to server: Permission denied
    Is the server running locally and accepting
    connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
Run Code Online (Sandbox Code Playgroud)

我不知道是什么造成的.

在搜索解决方案时,我更新了所有捆绑的宝石,更新的系统宝石,更新的MacPorts.没有快乐.

其他人在从OSX Leopard升级到Lion时报告了这个问题,因为混淆应该使用哪个版本的Postgres(即OSX版本或MacPorts版本).我已经在Lion运行了几个月,所以现在这应该发生似乎很奇怪.

如果不先了解问题是什么,我就不愿意过多地捣乱.如何有条不紊地调试?

如何确定我的系统中有多少版本的PostgreSQL,正在访问哪一个版本以及它所在的位置?如果使用错误的PostgreSQL,我该如何解决这个问题?

抱歉没有问题.我还在学习它是如何工作的!谢谢你的任何指示.

编辑

一些更新基于以下建议和评论.

我试图运行pg_lsclusters哪个返回command not found错误.

然后我尝试本地化我的pg_hba.conf文件并找到这三个示例文件:

/opt/local/share/postgresql84/pg_hba.conf.sample
/opt/local/var/macports/software/postgresql84/8.4.7_0/opt/local/share/postgresql84/pg_hba.conf.sample
/usr/share/postgresql/pg_hba.conf.sample
Run Code Online (Sandbox Code Playgroud)

所以我假设安装了3个版本的PSQL?Macports,OSX默认和???.

然后我搜索了ps -ef | grep postgres返回的launchctl启动脚本

0    56     1   0 11:41AM ??         0:00.02 /opt/local/bin/daemondo --label=postgresql84-server --start-cmd /opt/local/etc/LaunchDaemons/org.macports.postgresql84-server/postgresql84-server.wrapper start ; --stop-cmd /opt/local/etc/LaunchDaemons/org.macports.postgresql84-server/postgresql84-server.wrapper stop ; --restart-cmd /opt/local/etc/LaunchDaemons/org.macports.postgresql84-server/postgresql84-server.wrapper restart ; --pid=none
  500   372     1   0 11:42AM ??         0:00.17 /opt/local/lib/postgresql84/bin/postgres -D /opt/local/var/db/postgresql84/defaultdb
  500   766   372   0 11:43AM ??         0:00.37 postgres: writer process                                                                                                                                                                                                                                                                                                                   
  500   767   372   0 11:43AM ??         0:00.24 postgres: wal writer process                                                                                                                                                                                                                                                                                                               
  500   768   372   0 11:43AM ??         0:00.16 postgres: autovacuum launcher process                                                                                                                                                                                                                                                                                                      
  500   769   372   0 11:43AM ??         0:00.08 postgres: stats collector process                                                                                                                                                                                                                                                                                                          
  501  4497  1016   0 12:36PM ttys000    0:00.00 grep postgres
Run Code Online (Sandbox Code Playgroud)

我在http://pastebin.com/Gj5TpP62上发布了postgresql84-server.wrapper的内容.

我试图运行port load postgresql184-server但收到错误Error: Port postgresql184-server not found.

我仍然很困惑如何解决这个问题,并欣赏任何"傻瓜"指针.

谢谢!

EDIT2

这个问题在我遇到daemondo的问题后开始.我的本地Rails应用程序因"找不到daemondo gem"这样的应用程序错误而崩溃.然后,我进行了一系列捆绑更新,宝石更新,端口更新和brew更新,以尝试找到问题.

这个错误可能是daemondo的问题吗?

Phi*_*ing 66

这看起来像文件权限错误.Unix域套接字是文件,具有与其他任何用户权限相同的用户权限.看起来似乎尝试访问数据库的OSX用户没有访问套接字文件的文件权限.为了确认这一点,我已经对Ubuntu和psql进行了一些测试,试图生成相同的错误(包含在下面).

你需要检查的套接字文件和目录的权限/var/var/pgsql_socket.您的Rails应用程序(OSX用户)必须对这些目录具有执行(x)权限(最好授予每个人权限),并且套接字应具有完全权限(wrx).您可以使用它ls -lAd <file>来检查这些,如果它们中的任何一个是符号链接,您需要检查链接指向的文件或目录.

你可以为自己更改dir的权限,但是socket是由postgres配置的postgresql.conf.这可以在同一目录中找到pg_hba.conf(你必须找出哪一个).设置权限后,需要重新启动postgresql.

# postgresql.conf should contain...
unix_socket_directory = '/var/run/postgresql'       # dont worry if yours is different
#unix_socket_group = ''                             # default is fine here
#unix_socket_permissions = 0777                     # check this one and uncomment if necessary.
Run Code Online (Sandbox Code Playgroud)

编辑:

我已经在谷歌上进行了快速搜索,您可能希望查看它是否相关.这可能导致find您的配置文件失败的任何尝试.

http://www.postgresqlformac.com/server/howto_edit_postgresql_confi.html


错误消息:

在pg_hba.conf中找不到用户

psql: FATAL:  no pg_hba.conf entry for host "[local]", user "couling", database "main", SSL off
Run Code Online (Sandbox Code Playgroud)

用户密码验证失败:

psql: FATAL:  password authentication failed for user "couling"
Run Code Online (Sandbox Code Playgroud)

缺少unix套接字文件:

psql: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Run Code Online (Sandbox Code Playgroud)

Unix套接字存在,但服务器没有收听它.

psql: could not connect to server: Connection refused
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Run Code Online (Sandbox Code Playgroud)

unix套接字文件的文件权限错误:

psql: could not connect to server: Permission denied
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Run Code Online (Sandbox Code Playgroud)


wil*_*ser 41

我的直觉是,这是(再次)一个mac/OSX-thing:前端和后端假定unix-domain套接字的另一个位置(它作为一个集合点).

清单:

  • 是postgres运行:ps aux | grep postgres | grep -v grep应该做的伎俩
  • 套接字在哪里:( find / -name .s.PGSQL.5432 -ls套接字曾经在/ tmp;你可以开始在那里看)
  • 即使您找到(unix-domain)套接字,客户端也可以使用其他位置.(如果你混合发行版,或者你在某个地方安装了一个发行版并且在其他地方安装了另一个发行版(例如来自源代码),而客户端和服务器使用不同的rendez-vous地址,则会发生这种情况.

如果postgres正在运行,并且套接字实际存在,您可以使用:

  • psql -h /the/directory/where/the/socket/was/found mydbname

(试图连接到unix-domain套接字)

; 你现在应该得到psql提示:尝试\d然后\q退出.你也可以尝试:

  • psql -h localhost mydbname.

(尝试连接到localhost(127.0.0.1)

如果由于授权不足而导致这些尝试失败,则可以更改pg_hba.conf(和SIGHUP或重新启动)在这种情况下:还要检查日志.

一个类似的问题:无法让Postgres开始

注意:如果你可以进入psql提示符,这个问题的快速解决方法就是更改你的config/database.yml,添加:

host: localhost

或者您可以尝试添加:

host: /the/directory/where/the/socket/was/found

就我而言, host: /tmp

  • 将host:localhost添加到我的database.yml修复了我的问题.谢谢 :) (6认同)
  • 奇怪的.将"localhost"添加到我的database.yml也有效,但为什么呢?我以前没有那个,一切都很好.但我不认为我最近添加了任何东西,突然之间已经是这样了.> _ < (4认同)

Tom*_*son 26

尝试卸载pg gem(gem uninstall pg)然后重新安装 - 如果你使用bundler,那么bundle install,否则gem install pg.此外,确保路径选择正确的版本:Lion有一个posgresql版本(之前的版本没有),它可能在你本地安装版本之前的路径中(例如MacPorts,自制软件).

在我的情况下:自制的postgresql安装,更新postgresql,rails等,然后得到这个错误.卸载并重新安装pg gem为我做了这件事.

  • 捆绑exec gem pristine pg <==也会做的伎俩 (8认同)

Ben*_*ing 20

套接字文件的位置在编译时被烘焙到gem中.因此,您需要重建您的pg gem.

gem pristine pg
# or
bundle exec gem pristine pg
Run Code Online (Sandbox Code Playgroud)

这应解决该特定问题.


Nea*_*eal 16

如果您遇到类似的错误:

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"?
Run Code Online (Sandbox Code Playgroud)

这可能会成功(它为我做了):

initdb /usr/local/var/postgres -E utf8
Run Code Online (Sandbox Code Playgroud)

如果您不使用OSX/Brew,则指定的目录应该不同.

注意:这不是上面看到的确切错误消息,但此线程是该错误消息的第一个结果.

  • 我总是不时地遇到这个问题,但这是我第一次用这个解决方案解决它.此外,在运行`initb`之前必须使用`rm -fr/usr/local/var/postgres` (6认同)

Fir*_*gon 8

解决这个错误的原因是在postgres目录中删除了一个名为postmaster.pid的文件.请使用以下链接查看我的问题/答案,了解分步说明.我的问题与文件权限无关:

psql:无法连接到服务器:没有这样的文件或目录(Mac OS X)

回答这个问题的人们放弃了很多游戏,谢谢!我尽我所能


Fra*_*ans 7

这是我解决该错误消息的方式,部分基于wildplasser的答案.

find / -name .s.PGSQL.5432 -ls 2> /dev/null
  => ... /tmp/.s.PGSQL.5432
Run Code Online (Sandbox Code Playgroud)

所以,有我的套接字或其他什么,但客户端寻找它:

/var/run/postgresql/.s.PGSQL.5432
Run Code Online (Sandbox Code Playgroud)

所以简单地做一个符号链接/tmp/.s.PGSQL.5432:

sudo ln -s /tmp/.s.PGSQL.5432 /var/run/postgresql/.s.PGSQL.5432
Run Code Online (Sandbox Code Playgroud)

希望这对任何人都有帮助.这似乎有点不对,但嘿,它的确有效!

  • 哦,这个解决方案原来是'一次性'解决方案,我必须在每次重启后重做.对于Rails我找到了其他解决方案 - 修改数据库配置文件(下面添加了答案). (2认同)

Mar*_*rom 6

我升级到新的postgres后开始得到这个 - 我没有意识到我已经保存了数据文件.

首先,我尝试启动postgres服务器:

postgres -D /usr/local/var/postgres
Run Code Online (Sandbox Code Playgroud)

这就是我看到这个错误的方式

FATAL:  database files are incompatible with server
DETAIL:  The data directory was initialized by PostgreSQL version 9.0, which is not compatible with this version 9.3.5.
Run Code Online (Sandbox Code Playgroud)

所以我在SO上找到了这个答案 - 与不兼容性错误有关:https://serverfault.com/questions/342626/how-do-i-upgrade-postgresl-database-incompatibility-error

这就是修复它的原因

mv /usr/local/var/postgres /usr/local/var/postgres.old
initdb -D /usr/local/var/postgres
Run Code Online (Sandbox Code Playgroud)