I've been using Postgres without issue within Rails and have also been running psql without issue. However, I'm running into a problem trying to connect to the database with the 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.

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.

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.

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).

有帮助吗?

解决方案

You need to specify a host. It's trying to connect on a unix domain socket. The PG app doesn't listen on a unix socket (unless you configure it to) but runs on 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. See this post for more details and a possible solution.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top