简体   繁体   中英

Ruby/PG - Cannot connect to PostgreSQL

I've setup a new Linux Subsystem (Ubuntu) on Windows to run Ruby scripts, but the PG gem cannot connect to my PostgreSQL server. It seems like I can connect just fine using psql in the terminal (I think), but not using irb.


Problem:

If I run the following in IRB (within the Linux subsystem shell):

require 'pg'
PG.connect(:dbname=>"postgres")

I get the following error:

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/run/postgresql/.s.PGSQL.5432"?

However, if I use psql to to select the version(), it returns that just fine:

psql -p 5432 -h localhost -U postgres
postgres=# select version();

Returns:

PostgreSQL 10.1 on x86_64-pc-mingw64, compiled by gcc.exe (Rev5, Built by MSYS2 project) 4.9.2, 64-bit (1 row)

I should mention that I installed Postgres in my Windows environment, and not within the Linux subsystem - however, due to limitations in the subsystem, this seems to be the only way to do it from what I've read online.


I'm not sure if this belongs here or on Superuser or the unix/ubuntu SE, but my basic troubleshooting indicates that this is a problem with Ruby/pg gem, not the subsystem, so I'm posting my question here first.

The default connection is UNIX socket (a quasi-file, as you can see by the error message, /var/run/postgresql/.s.PGSQL.5432 ), not TCP socket (host:port). Pass your host/port explicitly to PG.connect .

PG.connect(dbname: "postgres", host: 'localhost', port: 5432)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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