From b6656fd874e6ba272b78855f83af12c54b96d2b7 Mon Sep 17 00:00:00 2001 From: BillionClaw Date: Wed, 18 Mar 2026 13:06:27 +0800 Subject: [PATCH] fix: respect DSN password over PGPASSWORD env var --- pgcli/main.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pgcli/main.py b/pgcli/main.py index 913228b33..662d4ab1e 100644 --- a/pgcli/main.py +++ b/pgcli/main.py @@ -632,7 +632,13 @@ def connect(self, database="", host="", user="", port="", passwd="", dsn="", **k # If password prompt is not forced but no password is provided, try # getting it from environment variable. if not self.force_passwd_prompt and not passwd: - passwd = os.environ.get("PGPASSWORD", "") + if dsn: + # Check if DSN contains a password - if so, don't use PGPASSWORD + parsed_dsn = conninfo_to_dict(dsn) + if "password" not in parsed_dsn: + passwd = os.environ.get("PGPASSWORD", "") + else: + passwd = os.environ.get("PGPASSWORD", "") # Prompt for a password immediately if requested via the -W flag. This # avoids wasting time trying to connect to the database and catching a