Thread: Customize psql prompt to show current_role
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
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.
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.
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 <ddevienne@gmail.com> 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
On Mon, Sep 23, 2024 at 2:22 PM Asad Ali <asadalinagri@gmail.com> wrote: > 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. Hi Ali. Doesn't seem to be working for me. Did I do something wrong? --DD ``` D:\>psql service=pau16 psql (17beta3, server 16.1) Type "help" for help. ddevienne=> \set current_role 'SELECT current_role;' ddevienne=> \set PROMPT1 '%n@%/ (%`current_role`)=%# ' 'current_role' is not recognized as an internal or external command, operable program or batch file. ddevienne@ddevienne ()=> select 1; ... 'current_role' is not recognized as an internal or external command, operable program or batch file. ddevienne@ddevienne ()=> ```
----- Mail original ----- > De: "Dominique Devienne" <ddevienne@gmail.com> > À: "Asad Ali" <asadalinagri@gmail.com> > Cc: pgsql-general@postgresql.org > Envoyé: Lundi 23 Septembre 2024 14:34:39 > Objet: Re: Customize psql prompt to show current_role > On Mon, Sep 23, 2024 at 2:22 PM Asad Ali <asadalinagri@gmail.com> wrote: >> 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. > > Hi Ali. Doesn't seem to be working for me. Did I do something wrong? --DD > > ``` > D:\>psql service=pau16 > psql (17beta3, server 16.1) > Type "help" for help. > > ddevienne=> \set current_role 'SELECT current_role;' > ddevienne=> \set PROMPT1 '%n@%/ (%`current_role`)=%# ' > 'current_role' is not recognized as an internal or external command, > operable program or batch file. > ddevienne@ddevienne ()=> select 1; > ... > 'current_role' is not recognized as an internal or external command, > operable program or batch file. > ddevienne@ddevienne ()=> > ``` Hello Dominique, I will use psql \gset command for this Regards Gilles
On 2024-09-23 14:22 +0200, Asad Ali wrote: > 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 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. > This approach requires you to execute it manually or include it in your > .psqlrc file for automatic loading with each session. > > On Mon, Sep 23, 2024 at 2:31 PM Dominique Devienne <ddevienne@gmail.com> > 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 -- Erik
On Mon, Sep 23, 2024 at 2:51 PM Erik Wienhold <ewie@ewie.name> wrote: > You could instead use this: > > SELECT current_role \gset > \set PROMPT1 '%n@%/ (%:current_role:)=%# ' > > 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
On Mon, Sep 23, 2024 at 8:22 AM Asad Ali <asadalinagri@gmail.com> wrote:
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.
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
On Mon, 2024-09-23 at 14:59 +0200, Dominique Devienne wrote: > On Mon, Sep 23, 2024 at 2:51 PM Erik Wienhold <ewie@ewie.name> wrote: > > You could instead use this: > > > > SELECT current_role \gset > > \set PROMPT1 '%n@%/ (%:current_role:)=%# ' > > > > 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. 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
On Mon, Sep 23, 2024 at 3:05 PM Laurenz Albe <laurenz.albe@cybertec.at> wrote: > On Mon, 2024-09-23 at 14:59 +0200, Dominique Devienne wrote: > > On Mon, Sep 23, 2024 at 2:51 PM Erik Wienhold <ewie@ewie.name> wrote: > > > You could instead use this: > > > > > > SELECT current_role \gset > > > \set PROMPT1 '%n@%/ (%:current_role:)=%# ' > > > > > > 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. > > To get the current role, psql would have to query the database whenever > 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
Laurenz Albe <laurenz.albe@cybertec.at> writes: > To get the current role, psql would have to query the database whenever > 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
On Mon, Sep 23, 2024 at 4:55 PM Tom Lane <tgl@sss.pgh.pa.us> wrote: > Laurenz Albe <laurenz.albe@cybertec.at> writes: > > To get the current role, psql would have to query the database whenever > > 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. 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
On 9/23/24 08:07, Dominique Devienne wrote: > On Mon, Sep 23, 2024 at 4:55 PM Tom Lane <tgl@sss.pgh.pa.us> wrote: >> Laurenz Albe <laurenz.albe@cybertec.at> writes: >>> To get the current role, psql would have to query the database whenever >>> 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. > > 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. For me that shows the user that connected(session_user) not the current_user. > > Therefore I want to respectfully re-iterate my interest in this enhancement. > > Thanks, --DD > > -- Adrian Klaver adrian.klaver@aklaver.com
Adrian Klaver <adrian.klaver@aklaver.com> writes: > On 9/23/24 08:07, Dominique Devienne wrote: >> I often resort to \conninfo, but it's less automatic and >> harder to visually parse (IMHO) compared to a custom ad-hoc prompt. > For me that shows the user that connected(session_user) not the > current_user. Worse than that: what it reports is libpq's PQuser(), that is the name that was used to log in with. But if what you want is the session_user, psql prompts already have %n for that. regards, tom lane
On Mon, Sep 23, 2024 at 5:16 PM Adrian Klaver <adrian.klaver@aklaver.com> wrote: > On 9/23/24 08:07, Dominique Devienne wrote: > > I often resort to \conninfo, but it's less automatic and > > harder to visually parse (IMHO) compared to a custom ad-hoc prompt. > For me that shows the user that connected(session_user) not the current_user. yeah, sure, I realized that just after sending my last post... I resort to using both \conninfo *and* `select current_role`. I used \conninfo to tell me which server I'm on (since not on the default prompt, before I played with it today), which session_user I'm connected with (ddevienne or dd_login in my recent example), and finally `select current_role` which matters in my use-cases (session_user or dd_admin or dd_user). I'd like not to have to type those commands/queries all the time, and have them in the prompt at all time. I get that it's not for everyone, nor even necessary all the time, but I sincerely think it is definitely useful. I'd be glad to use Pavel's proposed %N. --DD