Re: psycopg2: distinguishing connect failures through exception handling

From: "Karsten Hilbert" <Karsten(dot)Hilbert(at)gmx(dot)net>
To:
Cc: psycopg(at)postgresql(dot)org
Subject: Re: psycopg2: distinguishing connect failures through exception handling
Date: 2017-08-02 05:25:55
Message-ID: trinity-add0f822-577b-4923-8079-9bf6482faa08-1501651555817@3capp-gmx-bs61
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

<html><head></head><body><div style="font-family: Verdana;font-size: 12.0px;"><div>
<div>You probably want to look at pgping.</div>

<div>&nbsp;</div>

<div>Karsten</div>

<div>&nbsp;
<div name="quote" style="margin:10px 5px 5px 10px; padding: 10px 0 10px 10px; border-left:2px solid #C3D9E5; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">
<div style="margin:0 0 10px 0;"><b>Gesendet:</b>&nbsp;Mittwoch, 02. August 2017 um 06:20 Uhr<br/>
<b>Von:</b>&nbsp;&quot;Stephen Quintero&quot; &lt;30blows(at)gmail(dot)com&gt;<br/>
<b>An:</b>&nbsp;psycopg(at)postgresql(dot)org<br/>
<b>Betreff:</b>&nbsp;[psycopg] psycopg2: distinguishing connect failures through exception handling</div>

<div name="quoted-content">
<div>Hi Folks,
<div>&nbsp;</div>

<div>My first email here.&nbsp; I am using psycopg2 to remotely verify a postgres server is up, and separately that it is accessible given a particular user/password/database.&nbsp; This is done using <font face="monospace, monospace">psycopg2.connect(args...)</font>.&nbsp; The use case is automated deployment of the docker postgres container, where I wish to distinguish postgres service is up from postgres service cannot be accessed (e.g., with your user/password/database), from postgres service is down, in order to verify the container deployment.</div>

<div>&nbsp;</div>

<div>It seems the <font face="monospace, monospace">psycopg2.OperationalError</font> exception is thrown in a variety of circumstances: &nbsp;database does not exist, authentication failed, no password supplied, etc.&nbsp; But, the exception pgcode and pgerror are not set, evidently unless there is a successful connect.&nbsp; This leaves me in the position of distinguishing failures based on the error message string, e.g. <font color="#073763" face="monospace, monospace">fe_sendauth: no password supplied</font> or <font color="#073763" face="monospace, monospace">FATAL: &nbsp;password authentication failed for user &quot;&lt;val&gt;&quot;</font></div>

<div>&nbsp;</div>

<div>This does not seem like a reliable method, one certain to work in the future, and known to work now across myriad versions of postgres (outside the scope of my practical testing).</div>

<div>&nbsp;</div>

<div>I have these two questions.&nbsp; Is this understanding correct?&nbsp; Is there a reliable means to distinguish the types of connect failures using psycopg2?</div>

<div>&nbsp;</div>

<div>Thank you for your time.</div>

<div>&nbsp;</div>

<div>Best regards,</div>

<div>&nbsp;</div>

<div>Stephen Quintero</div>

<div>&nbsp;</div>
</div>
</div>
</div>
</div>
</div></div></body></html>

Attachment Content-Type Size
unknown_filename text/html 2.6 KB

In response to

Browse psycopg by date

  From Date Subject
Next Message Daniele Varrazzo 2017-08-02 14:10:15 Re: Changing set_session implementation
Previous Message Stephen Quintero 2017-08-02 04:20:44 psycopg2: distinguishing connect failures through exception handling