Skip to content

Add opt-in database URL loading from local .env files - #1665

Open
Suhail023 wants to merge 1 commit into
dbcli:mainfrom
Suhail023:feat/env-file-connection
Open

Suhail023 wants to merge 1 commit into
dbcli:mainfrom
Suhail023:feat/env-file-connection

Conversation

@Suhail023

@Suhail023 Suhail023 commented Nov 20, 2026 •

Copy link
Copy Markdown

Add --env-file PATH so a project can connect with pgcli --env-file .env or pgcli --env-file .env.local --ping, without extracting its URL in the shell. Fixes #1433.

Read the first non-empty DATABASE_URL, DATABASE_URI, DB_URL, or DB_URI using python-dotenv. Only the requested file is read, variable references stay literal, and no file settings are exported into the process environment. Missing or invalid URLs produce a Click error without printing credentials, and the file URL is redacted from the launch log.

Explicit connection arguments select the existing connection path instead of the file. Otherwise the file URL takes precedence over PGDATABASE and DSN; normal libpq defaults still apply to the URL. The existing URI connection path handles SSH tunneling, listing, pinging, and command execution. Document this behavior and add Bash completion. Raise the Click minimum to 8.0 for Context.get_parameter_source(), which distinguishes command-line arguments from environment defaults.

Validation:

  • Full PostgreSQL-backed unit suite on Linux, Python 3.10 and 3.14: 2,850 passed, 1 expected failure on each.
  • Connection tests with Click 8.0.0 and python-dotenv 1.0.0: 163 passed.
  • Behave terminal suite plus the PgBouncer feature: 34 scenarios passed, no failures; 8 existing skipped scenarios.
  • Ruff lint and formatting, changelog ReST validation, Bash syntax, and git diff --check pass.
  • Real .env.local smoke checks pass for --ping, --list, and SQL execution with conflicting environment defaults.
  • Windows: 2,847 passed, 1 skipped, 1 expected failure, with two existing file-lock failures reproduced on the untouched base commit (test_edit_named_query and test_pgcompleter_alias_uses_configured_alias_map).

Upstream workflow status: the pgcli and CodeQL runs currently require maintainer approval before any jobs run. Codex Review stopped before reviewing the patch because its action requires the triggering actor to have write access to this repository. These workflow permission gates require maintainer action; the local validation above completed successfully.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using environment variables to connect via ssh

1 participant