Search code examples
pythonpsycopg2peewee

Is connect_timeout a valid URL parameter acquiring a connection via Peewee's playhouse.db_url.connect?


I'm using Peewee as ORM and connect to a Postgres database (psycopg2) using the Playhouse extension db_url.connect. My URL is a vanilla postgresql://username:pass@host:port/dbname?options=... so not using pooling or anything advanced at the moment.

Some times when I call connect it hangs for a long time and doesn't come back. So I appended to my database URL the parameter &connect_timeout=3 meaning to try for at most 3 seconds and fail-fast with a timeout rather than hanging forever. However, I am not sure whether this argument is supported by Peewee/Playhouse/Psycopg2 ... can anyone confirm?

Furthermore, where can I find all the URL parameters supported by Peewee/Playhouse/Psycopg2?


Solution

  • The psycopg2 doc links in turn to the libpq list of supported parameters:

    https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS

    connect_timeout is supported by both peewee and psycopg2:

    >>> from playhouse.db_url import *
    >>> db = connect('postgresql://../peewee_test?connect_timeout=3')
    >>> conn = db.connection()
    >>> conn.get_dsn_parameters()
    {'user': 'postgres',
     'passfile': '...',
     'channel_binding': 'prefer',
     'connect_timeout': '3',  # Our connect timeout
     'dbname': 'peewee_test',
     'host': 'localhost',
     'port': '5432',
     ...}
    

    Peewee passes the parameters, including arbitrary ones like connect_timeout, back to the constructor of the DB-API Connection class.