3

I believe i have set up Pg properly, but my script doesn't seem to be connecting to the database. I am testing with:

$database="networkem";
$user="postgres";
$password="";
$host="localhost";

$dbh = DBI->connect("DBI:Pg:dbname=$dbname;host=$host", $user, $password);

My pg_hba reads:

host  all  postgres   127.0.0.1  255.255.255.255   trust

I can use psql just fine via command-line and have started postmaster with -i option. What am I missing?

I also tried with another user that works fine via psql:

$user="jimbo"; $password="p2ssw0rd";

with pg_hba reading:

host    all    jimbo   127.0.0.1    255.255.255.255    trust
1

5 Answers 5

11

Rather than play 20 questions to debug your setup, DBI->errstr will say why the connection failed.

my $dbh = DBI->connect(...) or die DBI->errstr;

Though if I had to guess... since Postgres authenticates based on host and login user, I suspect the confusion lies between the user name you're giving to the Postgres connection and the Unix user you're logged in as.

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

Comments

1

Besides Schwern's excellent response, you can also check PostgreSQL log which, depending on the options selected in postgresql.conf may tell you a lot about what was wrong.

Comments

1

It is recommended that you use the 'listen_addresses' configuration option in your postgresql.conf instead of '-i' on the command line. For example:

listen_addresses = '*'

Try executing the following command as the same user you are running your perl script with:

psql -U postgres -h localhost networkem

The '-h localhost' forces a network connection instead of a Unix socket connection. If that command works, your perl script should also work.

Comments

1

I had the same issue. The hint above for trying "-h localhost" confirmed that I had a problem connecting over the network.

Adding the following to pg_hba.conf fixed the problem.

host all postgres 127.0.0.1/32 trust

Comments

0

DBI may be connecting to either the IPv4 or IPv6 postgresql server interface, depending on how things are configured.

So you may need both of these lines in pg_hba.conf:

IPv4:

host  all  <user>  127.0.0.1/32  trust

IPv6:

host  all  <user>  ::1/128       trust

Comments

Your Answer

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