Subject: connect using env. variables

From: Hans Ginzel <[email-masked]>
Date: Tue, 9 Mar 2021 11:45:30 +0100

Please, there should be a possibility to call connect() without parameters
for connecting using environment variables (PGSERVICE, PGDATABASE,…)
or defaults ($USER@localhost/postgres).

Even pg uri postgres:// is valid,
https://www.postgresql.org/docs/current/libpq-connect.html.

>>> d = psycopg2.connect(**parse_dsn('postgres://'))
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "/usr/lib/python3/dist-packages/psycopg2/__init__.py", line 124, in connect
     raise TypeError('missing dsn and no parameters')
TypeError: missing dsn and no parameters
>>> d = psycopg2.connect(**parse_dsn(''))
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "/usr/lib/python3/dist-packages/psycopg2/__init__.py", line 124, in connect
     raise TypeError('missing dsn and no parameters')
TypeError: missing dsn and no parameters
>>> db = psycopg2.connect()
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "/usr/lib/python3/dist-packages/psycopg2/__init__.py", line 124, in connect
     raise TypeError('missing dsn and no parameters')
TypeError: missing dsn and no parameters

Thank you in advance,
Hans

---

From: Sebastiaan Mannem <[email-masked]>
Date: Tue, 9 Mar 2021 11:47:42 +0100

How about emptystring (e.a. '')?

Power to Postgres™!

Sebastiaan Mannem

Product Manager

M: +31 6 8252 1560

edbpostgres.com

EDB, Anthony Fokkerweg 1, 1059 CM Amsterdam, The Netherlands

---

From: Hans Ginzel <[email-masked]>
Date: Tue, 9 Mar 2021 12:06:19 +0100

Empty string does not work for **parse_dsn(), see example below.
H.

---

From: Daniele Varrazzo <[email-masked]>
Date: Tue, 9 Mar 2021 12:44:27 +0100

> Empty string does not work for **parse_dsn(), see example below.

It does: connect("", **parse_dsn())

-- Daniele

---

From: Hans Ginzel <[email-masked]>
Date: Tue, 9 Mar 2021 12:53:16 +0100

Thank you.

Is there a reason why connect() must get the empty string as parametr, please?

---

From: Daniele Varrazzo <[email-masked]>
Date: Tue, 9 Mar 2021 15:02:03 +0100

> Is there a reason why connect() must get the empty string as parametr, please?

No, and I'm happy to make the dsn string optional, as you requested in
github. psycopg3 already works this way and you can use connect(). I
just wanted to point out that you can do what you want to do already:
connecting using only the PG* env var or the parse_dsn() produce.

It's one of these things that is ugly but has been that way for so
long that we are used to it. Or better, which should have been fixed
when we introduced the possibility to pass both the dsn string and the
keywords to connect(), because in the past there was no parse_dsn()
and facility to merge the two.

Cheers

-- Daniele