1

I can connect fine with PHP and locally using Psql, but Perl does not.

my $dbh = DBI->connect("DBI:Pg:dbname=mydb,host=localhost:5432","user","pass",{'RaiseError' => 1});

I believe the error is because my socket is in tmp:

postgres@host/opt/psql/bin $ netstat -an | grep 5432
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN
tcp6       0      0 :::5432                 :::*                    LISTEN
unix  2      [ ACC ]     STREAM     LISTENING     24728255 /tmp/.s.PGSQL.5432
unix  3      [ ]         STREAM     CONNECTED     24729004 /tmp/.s.PGSQL.5432

And when I run my simple perl script it seems to look in /var/run:

./test.pl
DBI connect('dbname=mydb','user',...) failed: 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"? at ./test.pl line 6

I tried to simply create a symlink, but that doesn't seem to be working:

sudo ln -s /tmp/.s.PGSQL.5432 /var/run/postgresql/.s.PGSQL.5432
ln: failed to create symbolic link `/var/run/postgresql/.s.PGSQL.5432': No such file or directory

Some other simple stuff: pg_hba.conf trusts all localhost connections as well as those of my subnet.

postgresql.conf has the following:
listen_addresses = '*'

1
  • Try 127.0.0.1 instead of localhost. That should cause it to use TCP instead of a UNIX socket. Commented May 7, 2013 at 19:58

1 Answer 1

3

See http://www.postgresql.org/docs/9.2/static/libpq-connect.html#LIBPQ-CONNECT-HOST:

host

Name of host to connect to. If this begins with a slash, it specifies Unix-domain communication rather than TCP/IP communication; the value is the name of the directory in which the socket file is stored. The default behavior when host is not specified is to connect to a Unix-domain socket in /tmp (or whatever socket directory was specified when PostgreSQL was built). On machines without Unix-domain sockets, the default is to connect to localhost.

So a connection string that has host=/tmp should make your Pg client look in the right place (which is supposed to be the default anyway).

Sign up to request clarification or add additional context in comments.

2 Comments

That's it - I had tried to use the full path to the socket (/tmp/.s.PGSQL.5432) but that did not work. Using just /tmp does.
Removing the host parameter will help, too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.