Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

it stammers on error "could not connect"

#75

When it resolves the hostname, the error message is duplicated.
When you pass an IP address, the error message is correct.

(Ubuntu 64 bits)

>>> print __import__('sys').version
2.6.6 (r266:84292, Sep 15 2010, 16:22:56) 
[GCC 4.4.5]
>>> 
>>> import psycopg2 as pg
>>> print pg.__version__
2.4.2 (dt dec pq3 ext)
>>> 
>>> pg.connect('host=localhost')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
OperationalError: could not connect to server: Connection refused
    Is the server running on host "localhost" and accepting
    TCP/IP connections on port 24004?
could not connect to server: Connection refused
    Is the server running on host "localhost" and accepting
    TCP/IP connections on port 24004?

>>> pg.connect('host=127.0.0.1')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
OperationalError: could not connect to server: Connection refused
    Is the server running on host "127.0.0.1" and accepting
    TCP/IP connections on port 24004?

>>>

Reported by flox · November 5th, 2011 @ 10:54 PM

State: invalid
Milestone: none
Assigned to: nobody

Activity

  1. flox
    flox

    It's even worse on the @devel branch

    >>> import psycopg2 as pg
    >>> print pg.__version__
    2.4.3.dev0 (dt dec pq3 ext)
    >>> pg.connect('host=localhost')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    psycopg2.OperationalError: could not connect to server: Connection refused
        Is the server running on host "localhost" and accepting
        TCP/IP connections on port 5432?
    could not connect to server: Connection refused
        Is the server running on host "localhost" and accepting
        TCP/IP connections on port 5432?
    could not connect to server: Connection refused
        Is the server running on host "localhost" and accepting
        TCP/IP connections on port 5432?
    
    >>> pg.connect('host=127.0.0.1')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    psycopg2.OperationalError: could not connect to server: Connection refused
        Is the server running on host "127.0.0.1" and accepting
        TCP/IP connections on port 5432?
    

    November 6th, 2011 @ 01:33 AM

  2. Daniele Varrazzo
    Daniele Varrazzo

    This is just what is passed us by the libpq: it probably depends on what you have in the hosts file. What I get on my box recently switched to ubuntu 11.10 is:

    >>> cnn = psycopg2.connect('host=localhost port=5431')
    OperationalError: could not connect to server: Connection refused
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5431?
    could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5431?
    

    My hosts file says::

    127.0.0.1   localhost.localdomain    localhost
    ::1         bagheera    localhost6.localdomain6    localhost6
    

    can you try on your box to play a little with the /etc/hosts configuration, to see if the message changes, or to add a printf statement at connection to check the error message returned by the libpq? I say that's what the libpq says and is not mangled by psycopg. If i'm wrong I'll fix it, otherwise you should hear from the postgres developers.

    November 16th, 2011 @ 10:11 AM

  3. Daniele Varrazzo
    Daniele Varrazzo
    • State changed from new to invalid

    Please test applying the following patch:

    diff --git a/psycopg/connection_int.c b/psycopg/connection_int.c
    index 441d362..5aab2ea 100644
    --- a/psycopg/connection_int.c
    +++ b/psycopg/connection_int.c
    @@ -542,6 +542,7 @@ _conn_sync_connect(connectionObject *self)
         else if (PQstatus(pgconn) == CONNECTION_BAD)
         {
             Dprintf("conn_connect: PQconnectdb(%s) returned BAD", self->dsn);
    +        fprintf(stderr, "START\n%s\nEND\n", PQerrorMessage(pgconn));
             PyErr_SetString(OperationalError, PQerrorMessage(pgconn));
             return -1;
         }
    

    compile and try to connect to the wrong port. If you get a "stammering error" on stdout between a single pair START/END, it means that it is the libpq to stammer (but as you see from my output it could be that it is actually trying to open the connection in different ways). If you see several START/END pairs, it is our bug.

    Closing the bug unless further evidence is provided.

    November 16th, 2011 @ 11:54 PM

  4. flox
    flox

    Right, it seems that this message comes from libpq.

    I see that the message you have is more informative than the one I got: it shows which IP is tried on each tentative.

    Probably I don't use the same version of libpq as you.

    -- Florent

    November 17th, 2011 @ 08:52 PM