title: psycopg2.OperationalError: ERROR: pgbouncer cannot connect to server
body: <p>I am querying PostgreSQL using python and sometimes when I run the code, I get the following:</p>
  
  <pre><code>psycopg2.OperationalError: ERROR:  pgbouncer cannot connect to server
  </code></pre>
  
  <p>I'm looking for advice on how I can handle the exception.</p>
  
  <p>Code:</p>
  
  <pre><code>import psycopg2
  while True:
          conn = psycopg2.connect(database="****", user="**, password="****", host="bdl****", port="5400")
          cur = conn.cursor()
          print ("Opened database successfully")    
          try:
              cur.execute('''select * from abc;''')
              conn.commit()
              conn.close()
          except psycopg2.OperationalError:
              continue
          break
  </code></pre>