Re: pg_connect sometimes works sometimes not

From: Richard Huxton <dev(at)archonet(dot)com>
To: "pobox(at)verysmall(dot)org" <pobox(at)verysmall(dot)org>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: pg_connect sometimes works sometimes not
Date: 2007-04-26 08:31:21
Message-ID: 46306359.6020200@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

pobox(at)verysmall(dot)org wrote:
> Richard Huxton wrote:
>> Did you try pg_last_error()?
>
> pg_last_error() does not seem to work. It requires connection as
> parameter, so if pg_connect() fails - it has nothing to operate on.
>
> Or am I missing something?

No, I was. I've gone back and re-read your original message.

I'm a bit surprised you're not seeing an error message when the
connection fails. Try some code like this:

<?php

ini_set('track_errors','on');

$conn = @pg_connect("host=localhost dbname=nosuchdb user=richardh");

echo "Connection result: ";
print_r($conn);
echo "<hr>";

if ($conn===false) {
echo "Connection failed: ";
print_r($php_errormsg);
echo "<hr>";
}

?>

Without the "track_errors" and @ on the front of pg_connect you should
get a php error. You might want an error-handling function.

>> Are you logging connection attempts/failures? Details in the manuals.
>
> Checked the manual, but found only parameter to log the successful
> connections.
>
> Is there such to log the failed connection attempts (incl. the reason)?

It's an error, so you'll see an error logged.

2007-04-26 09:16:00 BST nosuchdb 1 FATAL: database "nosuchdb" does not
exist

Of course, if you're connecting to the wrong port, or wrong machine then
the server will never see the connection, so it can't log that.

--
Richard Huxton
Archonet Ltd

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message pobox@verysmall.org 2007-04-26 08:38:11 Re: pg_connect sometimes works sometimes not
Previous Message Richard Huxton 2007-04-26 08:23:54 Re: Schema as versioning strategy