Thread: pg_connect sometimes works sometimes not

pg_connect sometimes works sometimes not

From
"pobox@verysmall.org"
Date:
Hello,

we migrated a php code from FreeBSD 5.x, PostgreSQL 8.x and php 4.x - to
the latest versions of these, keeping the configuration options. Now
pg_connect started to fail on irregular intervals for no obvious reason.
Before we had a problem with the number of connections, but we monitored
them and they are less than 10 (out of 100 available).

What other variable (which are sometimes there, sometimes not) reasons
there can be pg_connect to fail?

Thank you,
Iv

Re: pg_connect sometimes works sometimes not

From
Richard Huxton
Date:
pobox@verysmall.org wrote:
> What other variable (which are sometimes there, sometimes not) reasons
> there can be pg_connect to fail?

What result-code/error do you get? What do your logs show?

--
   Richard Huxton
   Archonet Ltd

Re: pg_connect sometimes works sometimes not

From
"pobox@verysmall.org"
Date:
Richard Huxton wrote:
> pobox@verysmall.org wrote:
>> What other variable (which are sometimes there, sometimes not) reasons
>> there can be pg_connect to fail?
>
> What result-code/error do you get? What do your logs show?

I have -

$connection = pg_connect("$host $db $user $pass");

When I get the error it is because $connection is 'false'.

Thought of using pg_result_error - but it seems applicable only to
queries (i.e. with pg_query or pg_execute). How can I get an error code?

PostgreSQL is instructed to log into the syslog, which is
/var/log/messages. There are only two type of things there from today -

[1-1] WARNING: nonstandard use of \\ in a string literal at character XXX

[1-2] HINT: Use the escape string syntax for backslashes, e.g., E'\\'.

But it does not seem like any of these are related to pg_connect, or am
I wrong (I guess they are related to bad code somewhere).

Thank you,
Iv


Re: pg_connect sometimes works sometimes not

From
Richard Huxton
Date:
pobox@verysmall.org wrote:
> Richard Huxton wrote:
>> pobox@verysmall.org wrote:
>>> What other variable (which are sometimes there, sometimes not)
>>> reasons there can be pg_connect to fail?
>>
>> What result-code/error do you get? What do your logs show?
>
> I have -
>
> $connection = pg_connect("$host $db $user $pass");
>
> When I get the error it is because $connection is 'false'.
>
> Thought of using pg_result_error - but it seems applicable only to
> queries (i.e. with pg_query or pg_execute). How can I get an error code?

Did you try pg_last_error()?

> PostgreSQL is instructed to log into the syslog, which is
> /var/log/messages. There are only two type of things there from today -
>
> [1-1] WARNING: nonstandard use of \\ in a string literal at character XXX
>
> [1-2] HINT: Use the escape string syntax for backslashes, e.g., E'\\'.
>
> But it does not seem like any of these are related to pg_connect, or am
> I wrong (I guess they are related to bad code somewhere).

Are you logging connection attempts/failures? Details in the manuals.

--
   Richard Huxton
   Archonet Ltd

Re: pg_connect sometimes works sometimes not

From
Raymond O'Donnell
Date:
On 25/04/2007 15:50, pobox@verysmall.org wrote:

> Thought of using pg_result_error - but it seems applicable only to
> queries (i.e. with pg_query or pg_execute). How can I get an error code?

pg_last_error()

http://www.php.net/manual/en/function.pg-last-error.php

Ray.

---------------------------------------------------------------
Raymond O'Donnell, Director of Music, Galway Cathedral, Ireland
rod@iol.ie
---------------------------------------------------------------

Re: pg_connect sometimes works sometimes not

From
"pobox@verysmall.org"
Date:
Richard Huxton wrote:
 > Did you try pg_last_error()?

No. Will try now.

 > Are you logging connection attempts/failures? Details in the manuals.

Understood.

Thank you very much!
Iv

Re: pg_connect sometimes works sometimes not

From
Martijn van Oosterhout
Date:
On Wed, Apr 25, 2007 at 04:50:46PM +0200, pobox@verysmall.org wrote:
> Richard Huxton wrote:
> >pobox@verysmall.org wrote:
> >>What other variable (which are sometimes there, sometimes not) reasons
> >>there can be pg_connect to fail?
> >
> >What result-code/error do you get? What do your logs show?
>
> I have -
>
> $connection = pg_connect("$host $db $user $pass");
>
> When I get the error it is because $connection is 'false'.
>
> Thought of using pg_result_error - but it seems applicable only to
> queries (i.e. with pg_query or pg_execute). How can I get an error code?

In the C interface it's called PQerrorMessage(). I'm sure PHP has an
equivalent.

Have a nice day,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> From each according to his ability. To each according to his ability to litigate.

Attachment

Re: pg_connect sometimes works sometimes not

From
"pobox@verysmall.org"
Date:
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?

> 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)?

Thank you,
Iv

Re: pg_connect sometimes works sometimes not

From
Richard Huxton
Date:
pobox@verysmall.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

Re: pg_connect sometimes works sometimes not

From
"pobox@verysmall.org"
Date:
Richard Huxton wrote:
> Try some code like this:

OK I'll try it now and write back.

Thanks!
Iv