简体   繁体   English

如何通过Ruby的pg gem连接到Postgres数据库(Rails外部)

[英]How to connect to Postgres database through Ruby's pg gem (outside of Rails)

I've been using Postgres without issue within Rails and have also been running psql without issue. 我一直在Rails中使用Postgres而没有问题,也一直在运行psql而没有问题。 However, I'm running into a problem trying to connect to the database with the pg gem. 但是,我在尝试使用pg gem连接数据库时遇到问题。

The following code: 如下代码:

PG.connect

generates the error/output: 产生错误/输出:

PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?

The result is the same whether I specify various dbname: values or not or just let this default. 无论我是否指定各种dbname:值,还是只使用默认值,结果都是相同的。

When I click on the little elephant icon at the top of my screen, it says "Running on port 5432" and, as I said, I can connect with psql without a problem. 当我单击屏幕顶部的小象图标时,它显示“正在端口5432上运行”,并且正如我所说,我可以毫无问题地连接psql

I found the very similar cannot connect to Postgres (pg) database from my Ruby Script using gem "pg"(This is not rails, just pure ruby) , but the answer simply said Rails works fine. 我发现非常相似的无法使用gem“ pg”从我的Ruby Script连接到Postgres(pg)数据库(这不是Rails,仅仅是纯红宝石) ,但是答案只是说Rails可以正常工作。

Any guidance would be appreciated. 任何指导将不胜感激。 (Note: I realize I haven't entered any name/password info, but I don't have to enter than when I run psql and I gather it's not getting to the point of authentication). (注意:我意识到我没有输入任何名称/密码信息,但是我运行的时候没有输入psql而我收集到的信息并没有达到验证的目的)。

You need to specify a host. 您需要指定一个主机。 It's trying to connect on a unix domain socket. 它正在尝试在Unix域套接字上进行连接。 The PG app doesn't listen on a unix socket (unless you configure it to) but runs on localhost:5432. PG应用程序不会在unix套接字上监听(除非您将其配置为),而是在localhost:5432上运行。

 conn = PG.connect( host: 'localhost' )

It is quite likely that the actual problem is that your 'pg' gem has linked to an older version of a libpq dynamic library. 实际问题很可能是您的“ pg” gem已链接到旧版本的libpq动态库。 See this post for more details and a possible solution. 请参阅此帖子以获取更多详细信息和可能的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM