From c0d184cf0a9369f3c8d8f0a87b07e2c377c7776d Mon Sep 17 00:00:00 2001 From: Charbel Jacquin <11244811+charbeljc@users.noreply.github.com> Date: Sat, 1 Nov 2025 01:31:09 +0100 Subject: [PATCH 1/2] Allow per dsn_alias ssh tunnel selection --- pgcli/main.py | 9 ++++++++- pgcli/pgclirc | 9 +++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/pgcli/main.py b/pgcli/main.py index 5edfb909f..0b4b64f59 100644 --- a/pgcli/main.py +++ b/pgcli/main.py @@ -290,6 +290,7 @@ def __init__( self.prompt_app = None + self.dsn_ssh_tunnel_config = c.get("dsn ssh tunnels") self.ssh_tunnel_config = c.get("ssh tunnels") self.ssh_tunnel_url = ssh_tunnel_url self.ssh_tunnel = None @@ -643,6 +644,12 @@ def should_ask_for_password(exc): if "port" in parsed_dsn: port = parsed_dsn["port"] + if self.dsn_alias and self.dsn_ssh_tunnel_config and not self.ssh_tunnel_url: + for dsn_regex, tunnel_url in self.dsn_ssh_tunnel_config.items(): + if re.search(dsn_regex, self.dsn_alias): + self.ssh_tunnel_url = tunnel_url + break + if self.ssh_tunnel_config and not self.ssh_tunnel_url: for db_host_regex, tunnel_url in self.ssh_tunnel_config.items(): if re.search(db_host_regex, host): @@ -1540,8 +1547,8 @@ def cli( fg="red", ) sys.exit(1) - pgcli.connect_uri(dsn_config) pgcli.dsn_alias = dsn + pgcli.connect_uri(dsn_config) elif "://" in database: pgcli.connect_uri(database) elif "=" in database and service is None: diff --git a/pgcli/pgclirc b/pgcli/pgclirc index ab49362f9..63ccdaf30 100644 --- a/pgcli/pgclirc +++ b/pgcli/pgclirc @@ -258,3 +258,12 @@ float = "" [column_date_formats] # use strftime format, e.g. # created = "%Y-%m-%d" + +# Per host ssh tunnel configuration +[ssh tunnels] +# ^example.*\.host$ = myuser:mypasswd@my.tunnel.com:4000 +# .*\.net = another.tunnel.com + +# Per dsn_alias ssh tunnel configuration +[dsn ssh tunnels] +# ^example_dsn$ = myuser:mypasswd@my.tunnel.com:4000 From 0c12e6e53957f61b91dcf423bfd429e30f81729f Mon Sep 17 00:00:00 2001 From: Charbel Jacquin <11244811+charbeljc@users.noreply.github.com> Date: Sat, 1 Nov 2025 01:36:23 +0100 Subject: [PATCH 2/2] update AUTHORS & changelog.rst --- AUTHORS | 1 + changelog.rst | 1 + 2 files changed, 2 insertions(+) diff --git a/AUTHORS b/AUTHORS index ef4a014c7..771de13f1 100644 --- a/AUTHORS +++ b/AUTHORS @@ -143,6 +143,7 @@ Contributors: * Doug Harris (dougharris) * Jay Knight (jay-knight) * fbdb + * Charbel Jacquin (charbeljc) Creator: -------- diff --git a/changelog.rst b/changelog.rst index c82dce084..96eefd747 100644 --- a/changelog.rst +++ b/changelog.rst @@ -8,6 +8,7 @@ Features: * Provide `init-command` in the config file * Support dsn specific init-command in the config file * Add suggestion when setting the search_path +* Allow per dsn_alias ssh tunnel selection Internal: ---------