Subject: Re: Customize psql prompt to show current_role

From: Asad Ali <[email-masked]>
Date: Mon, 23 Sep 2024 17:22:29 +0500

Hi Dominique,

There is no direct prompt escape sequence like %n for displaying the
current_role in the psql prompt. However, you can work around this by using
a \set command to define a custom prompt that includes the result of
current_role.
You can use the following command to set your psql PROMPT1 to include both
the session_user and current_role:

Here’s how you can achieve this:

You can define a function in your psqlrc file that captures the current
role.

Use \set to set a custom prompt that includes both the session user (%n)
and the current_role.
Here's an example of how you can do it:

Edit your .psqlrc file to include a custom query and set a prompt:

-- Query to set the current role into a psql variable
\set current_role 'SELECT current_role;'

-- Define a custom prompt with both the session user and the current role
\set PROMPT1 '%n@%/ (%`current_role`)=%# '

Load the .psqlrc or start a new psql session, and your prompt will now show
the session user and current role.

The key here is that \set allows you to run SQL commands within the prompt,
which can be used to extract the current_role.

This approach requires you to execute it manually or include it in your
.psqlrc file for automatic loading with each session.

I hope this helps!

Best regards,
Asad Ali

On Mon, Sep 23, 2024 at 2:31 PM Dominique Devienne <[email-masked]>
wrote:

> Hi. I've successfully customized my psql PROMPT1,
> using %n for session_user, but I'd like to see
> current_role as well. And I can't seem to find a way.
>
> I didn't find a direct \x for it.
> I didn't find a %'X' variable for it.
> I didn't find a command to %`X` either.
> (and X = `select current_role` does not work).
>
> Surely there's a way, no? Thanks, --DD
>
>
>

---

From: Erik Wienhold <[email-masked]>
Date: Mon, 23 Sep 2024 14:51:08 +0200

> which can be used to extract the current_role.

This is wrong.  First of all, \set does not run any SQL commands.
Secondly, %`current_role` will be sent to the shell and execute command
current_role.

You could instead use this:

    SELECT current_role \gset
    \set PROMPT1 '%n@%/ (%:current_role:)=%# '

But that won't work with subsequent SET ROLE commands.

-- 
Erik

---

From: Dominique Devienne <[email-masked]>
Date: Mon, 23 Sep 2024 14:59:43 +0200

> But that won't work with subsequent SET ROLE commands.

Bummer... That was kinda the point, that it updates automatically.

Then I'd like to respectfully request a small enhancement to psql
to support a new \r (or some other letter) for current_role in the prompt.

Thanks, --DD

---

From: Greg Sabino Mullane <[email-masked]>
Date: Mon, 23 Sep 2024 09:02:57 -0400

> a \set command to define a custom prompt that includes the result of

Please do not use chatgpt or other LLMs to answer questions on this mailing
list! If you had even done the bare minimum of trying your own
lazily-generated answer, you would have found it did not work.

Greg

---

From: Laurenz Albe <[email-masked]>
Date: Mon, 23 Sep 2024 15:05:10 +0200

> to support a new \r (or some other letter) for current_role in the prompt.

To get the current role, psql would have to query the database whenever
it displays the prompt.  That would be rather expensive...

Yours,
Laurenz Albe

---

From: Dominique Devienne <[email-masked]>
Date: Mon, 23 Sep 2024 15:08:47 +0200

> it displays the prompt.  That would be rather expensive...

Hi Laurenz. Sure, although expensive might be a bit strong a word.
But that would be opt-in anyway, so just a small warning in the doc
about that new option would suffice, no? --DD

---

From: Tom Lane <[email-masked]>
Date: Mon, 23 Sep 2024 10:54:55 -0400

> it displays the prompt.  That would be rather expensive...

See previous discussion:

https://www.postgresql.org/message-id/flat/CAFj8pRBFU-WzzQhNrwRHn67N0Ug8a9-0-9BOo69PPtcHiBDQMA%40mail.gmail.com

At the time I didn't like the idea too much, but now that we've seen
a second independent request, maybe our opinion of its value should
go up a notch.

			regards, tom lane

---

From: Dominique Devienne <[email-masked]>
Date: Mon, 23 Sep 2024 17:07:47 +0200

> a second independent request, maybe our opinion of its value should

Thanks for the reference Tom. I don't follow -hackers, so missed it.

Obviously I disagree with your "it's not useful enough" comment :)

I often lose track of which ROLE is active, in my many
long-lived psql sessions, on various servers (mainly v14 and v16 these days),
especially during my recent struggle to adapt our system to v16.
I often resort to \conninfo, but it's less automatic and
harder to visually parse (IMHO) compared to a custom ad-hoc prompt.

Therefore I want to respectfully re-iterate my interest in this enhancement.

Thanks, --DD