Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ssi5W-00AJ8H-Du for pgsql-general@arkaria.postgresql.org; Mon, 23 Sep 2024 12:22:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1ssi5V-007hIh-Rq for pgsql-general@arkaria.postgresql.org; Mon, 23 Sep 2024 12:22:45 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ssi5V-007hIW-GH for pgsql-general@lists.postgresql.org; Mon, 23 Sep 2024 12:22:45 +0000 Received: from mail-ed1-x52b.google.com ([2a00:1450:4864:20::52b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1ssi5R-000fGd-KL for pgsql-general@postgresql.org; Mon, 23 Sep 2024 12:22:44 +0000 Received: by mail-ed1-x52b.google.com with SMTP id 4fb4d7f45d1cf-5c24c92f699so4216878a12.2 for ; Mon, 23 Sep 2024 05:22:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727094162; x=1727698962; darn=postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=mxHuS+LHdDklZuAJ63GCAs793Gj3XcNA8DUR/MEpBwc=; b=l2C7tXYPPWOYW+owO5znVzFmpithbrlP1uR5voFR7ZRtYpMm+YhH/ppUrBTvhsjjCH ZOK+1EJKwp3tUrEhwEKE/WQbSivYgB6NHnkTGmY/6UpJPmIoFKivJ5500PRzqAymcziJ 34IZw0u6YTJWdD3qc4ulr6RaUKDH2NlqPduWQGlG04eGsel/FYrZu2LyYedQosLBRr+N nEwIQXWFsykGZjLV1DskcO1xjJIylJazuHc+nFcgsb4507u5kOqYjWa8U6I37bgOgpW9 bsXlT2m+8L8SlMLyYeuYlMpv0ub3Fi0aux++Had+Uf2oUEKGavV1tDLKjeTdXVSN1D6W /E0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727094162; x=1727698962; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=mxHuS+LHdDklZuAJ63GCAs793Gj3XcNA8DUR/MEpBwc=; b=XfQJ3u5mJ2NqfF9PR3qLaFxXyBe9cmTUYw2OKslZMJ51Ga92ad+j4ORrh0CqdRcBIh AsR/PvvLA0JRNO9FC6YDJtnRlWrrsp+TbK7MPdJ9h5JiITt1ggUDaf+lxV945haEh5Lm U3gMiJ5gqhYGRa0ook5kY7NLHo0d3NjHqrZubBu1l172Xz1yDERpFlTOrIr3qnlljedX 74xkiTQs9fu/x8/ATHtTUp5/+MLJIThkb9KOdwAAGwpDHAqT6L947MXPoDEF+3DAV99X sZ3hmIshOhxX9y2p2smKg2xbvW24cntIpuE0i/S1HYWDGXfrer94WbI7mVlkowVIKkEk I+QQ== X-Gm-Message-State: AOJu0YzetIgKVe2PwTMv0TUZ8mcf5bjgBu8Ek5NEg70p/A1AhosmUI6Q W7Yf/8kUJs5Q1nqRDB3bfwp7VfgHar010vRcIX9tzOCSFM/MnxK2lYSuaUG+AsXDkN0BxLcRFO5 y0G8zPZ1B5ge/hsj6tpBL94cHP6A= X-Google-Smtp-Source: AGHT+IGJk/TVw8+SIj/YWAedUplMuWC31ehMlhWT8JJQI3WW17y6u0k+VncYHo4Tnz4CXhqR+OMB8AYAXHsKkGH4GZc= X-Received: by 2002:a05:6402:27d4:b0:5c2:75d3:fbf7 with SMTP id 4fb4d7f45d1cf-5c464db3de0mr11532834a12.14.1727094161824; Mon, 23 Sep 2024 05:22:41 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Asad Ali Date: Mon, 23 Sep 2024 17:22:29 +0500 Message-ID: Subject: Re: Customize psql prompt to show current_role To: Dominique Devienne Cc: pgsql-general@postgresql.org Content-Type: multipart/alternative; boundary="0000000000000265a30622c87219" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000000265a30622c87219 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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=E2=80=99s 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`)=3D%# ' 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=E2=80=AFPM Dominique Devienne 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 =3D `select current_role` does not work). > > Surely there's a way, no? Thanks, --DD > > > --0000000000000265a30622c87219 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi Dominique,

There is no di= rect 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 d= efine a custom prompt that includes the result of current_role.
You can use the following command to set your psql PROMPT1 to include bo= th the session_user and current_role:

Here=E2=80= =99s how you can achieve this:

You can define a function in your psq= lrc file that captures the current role.

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

Edit your .psqlrc fil= e to include a custom query and set a prompt:

-- Query to set the cu= rrent role into a psql variable
\set current_role 'SELECT current_ro= le;'

-- Define a custom prompt with both the session user and th= e current role
\set PROMPT1 '%n@%/ (%`current_role`)=3D%# '
<= br>
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 th= at \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 e= ach session.

I hope this helps!

=
Best regards,
Asad Ali


On Mon, Sep 23, 2024= at 2:31=E2=80=AFPM Dominique Devienne <ddevienne@gmail.com> wrote:
Hi. I've successfully customized my psql PROM= PT1,
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 =3D `select current_role` does not work).

Surely there's a way, no? Thanks, --DD


--0000000000000265a30622c87219--