0

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.

1 Answer 1

3

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) 
Sign up to request clarification or add additional context in comments.

1 Comment

That was it indeed! I thought I had tried defining the host and port within the first hour of banging my head against the wall trying to find the solution, but I guess I hadn't. Thank you very much.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.