Psycopg2: Connect() with environment and no parameters

Created on 2 Sep 2018  路  4Comments  路  Source: psycopg/psycopg2

Hi

According to the documentation and tests, connect() needs a DSN string or arguments to specify connection information.
But the documentation also says that we can use environment variables to specify such information.
That's what I want to do, but I'm forced to give at least one parameter to connect() or it will fail with:

// everything is set in the environment (PG* variables)
con = psycopg2.connect()
File "/usr/lib/python3.6/site-packages/psycopg2/__init__.py", line 127, in connect
raise TypeError('missing dsn and no parameters')
TypeError: missing dsn and no parameters

// this works
con = psycopg2.connect('dbname={}'.format(os.getenv('PGDATABASE')))

Is there a cleaner solution than reading one of the variable to feed connect()?
Why "no parameters" raise an error?

Thanks

Most helpful comment

Use psycopg2.connect('') with an empty string.

All 4 comments

Use psycopg2.connect('') with an empty string.

Make sense, thanks

Use psycopg2.connect('') with an empty string.

Does this mean it automatically picks the fields from the env?

Yes it does

Was this page helpful?
0 / 5 - 0 ratings