Skip to content

Commit 3c3776c

Browse files
Show PostgreSQL service names in the prompt (dbcli#1632)
* Expose the PostgreSQL service name in prompts Fixes dbcli#1624. Implemented and validated with Codex. * Move changelog entry --------- Co-authored-by: Irina Truong <[email protected]>
1 parent baf0f01 commit 3c3776c

5 files changed

Lines changed: 39 additions & 1 deletion

File tree

‎AUTHORS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ Contributors:
157157
* Pieter Ouwerkerk (pouwerkerk)
158158
* VXNCXNX
159159

160+
* jackwalkerlabs (Codex-assisted)
161+
160162
Creator:
161163
--------
162164
Amjith Ramanujam

‎changelog.rst‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Upcoming
22
========
33

4+
Features:
5+
---------
6+
* Add a ``\service`` prompt token for connections made using ``service=...``
7+
or ``PGSERVICE``. For example, set ``prompt = '\service> '``.
48

59
4.7.1 (2026-09-20)
610
==================
@@ -17,7 +21,6 @@ Bug fixes:
1721
**Brown bag release:** this version shows (in `pgcli --version`) as
1822
"4.6.0" instead of "4.7.0".
1923

20-
2124
Features:
2225
---------
2326
* Add support for `single-command` to run a SQL command and exit.

‎pgcli/main.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ def __init__(
237237
self.never_passwd_prompt = never_passwd_prompt
238238
self.pgexecute = pgexecute
239239
self.dsn_alias = None
240+
self.service_name = None
240241
self.watch_command = None
241242
self.force_destructive = force_destructive
242243

@@ -703,6 +704,7 @@ def connect_service(self, service, user):
703704
port=service_config.get("port"),
704705
passwd=service_config.get("password"),
705706
)
707+
self.service_name = service or os.getenv("PGSERVICE")
706708

707709
def connect_uri(self, uri):
708710
kwargs = conninfo_to_dict(uri)
@@ -869,6 +871,7 @@ def should_ask_for_password(exc):
869871
sys.exit(1)
870872

871873
self.pgexecute = pgexecute
874+
self.service_name = None
872875

873876
def handle_editor_command(self, text):
874877
r"""
@@ -1494,6 +1497,7 @@ def get_completions(self, text, cursor_positition):
14941497
def get_prompt(self, string):
14951498
# should be before replacing \\d
14961499
string = string.replace("\\dsn_alias", self.dsn_alias or "")
1500+
string = string.replace("\\service", self.service_name or "")
14971501
string = string.replace("\\t", self.now.strftime("%x %X"))
14981502
string = string.replace("\\u", self.pgexecute.user or "(none)")
14991503
string = string.replace("\\H", self.pgexecute.host or "(none)")

‎pgcli/pgclirc‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ verbose_errors = False
188188
# \n - Newline
189189
# \T - Transaction status: '*' if in a valid transaction, '!' if in a failed transaction, '?' if disconnected, empty otherwise
190190
# \dsn_alias - name of dsn connection string alias if -D option is used (empty otherwise)
191+
# \service - PostgreSQL service name (empty when not connected using a service)
191192
# \x1b[...m - insert ANSI escape sequence
192193
# eg: prompt = '\x1b[35m\u@\x1b[32m\h:\x1b[36m\d>'
193194
prompt = '\u@\h:\d> '

‎tests/test_main.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,34 @@ def test_get_prompt_with_transaction_status(transaction_indicator, expected):
768768
assert result == expected
769769

770770

771+
@pytest.mark.parametrize("service_name", ["reporting", None])
772+
def test_get_prompt_service_name(service_name):
773+
cli = PGCli()
774+
cli.pgexecute = mock.MagicMock(user="user", host="localhost", short_host="localhost", dbname="db", transaction_indicator="")
775+
cli.service_name = service_name
776+
assert cli.get_prompt("\\service> ") == f"{service_name or ''}> "
777+
778+
779+
@pytest.mark.parametrize("service", ["reporting", None])
780+
def test_connect_service_remembers_name(tmp_path, monkeypatch, service):
781+
cli = PGCli(pgclirc_file=str(tmp_path / "rcfile"))
782+
service_file = tmp_path / "pg_service.conf"
783+
service_file.write_text("[reporting]\nhost=localhost\n")
784+
monkeypatch.setenv("PGSERVICEFILE", str(service_file))
785+
monkeypatch.setenv("PGSERVICE", "reporting")
786+
with mock.patch.object(cli, "connect"):
787+
cli.connect_service(service, None)
788+
assert cli.service_name == "reporting"
789+
790+
791+
def test_direct_connection_clears_service_name(tmp_path):
792+
cli = PGCli(pgclirc_file=str(tmp_path / "rcfile"))
793+
cli.service_name = "reporting"
794+
with mock.patch("pgcli.main.PGExecute"):
795+
cli.connect(database="db", user="user", host="localhost")
796+
assert cli.service_name is None
797+
798+
771799
def test_get_prompt_transaction_status_in_full_prompt():
772800
cli = PGCli()
773801
cli.pgexecute = mock.MagicMock()

0 commit comments

Comments
 (0)