for i in range(retries): try: cnx = pgdb._connect_(cstr, dbhost, dbport, dbopt, dbtty, dbuser, dbpasswd) break except pgdb.InternalError, e: if 'timeout expired' in str(e): logger.warning('Timeout expired connecting to %s, attempt %d/%d' % (dbbase, i+1, retries)) continue raise if cnx is None: raise ConnectionError('Failed to connect to %s' % dbbase) conn = pgdb.pgdbCnx(cnx) #by default, libpq will print WARNINGS to stdout if not verbose: cursor=conn.cursor() cursor.execute("SET CLIENT_MIN_MESSAGES='ERROR'") conn.commit() cursor.close() # set client encoding if needed if encoding: cursor=conn.cursor() cursor.execute("SET CLIENT_ENCODING='%s'" % encoding) conn.commit() cursor.close()