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 1ssiXC-00AMwF-6V for pgsql-general@arkaria.postgresql.org; Mon, 23 Sep 2024 12:51:23 +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 1ssiXB-008Fla-Cn for pgsql-general@arkaria.postgresql.org; Mon, 23 Sep 2024 12:51:21 +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 1ssiXA-008FlR-V5 for pgsql-general@lists.postgresql.org; Mon, 23 Sep 2024 12:51:21 +0000 Received: from mout-u-204.mailbox.org ([80.241.59.204]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ssiX2-000fTD-GM for pgsql-general@postgresql.org; Mon, 23 Sep 2024 12:51:20 +0000 Received: from smtp1.mailbox.org (smtp1.mailbox.org [10.196.197.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-u-204.mailbox.org (Postfix) with ESMTPS id 4XC2t61wBHz9sQc; Mon, 23 Sep 2024 14:51:10 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ewie.name; s=MBO0001; t=1727095870; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=qkAcpdaatOpnws4D0EFKC+IBo0DRgw7FEzKi5JO2/uw=; b=kga3GJLPur5OATCol540fE+GkzbClrL1MQ8IU4rxYIgIJcDZDgufefrwOWcgTaWIssa5ry zVAotBD4ne82VHpkgENh52kASdVOvmS4hB+VwqCs7JkvVFuDIafhFUdKB1KdIkePEQSUAx jNEiebhxQmy6+9tDhHF0DLEJPerlqP/ujphvB12C0NzzgGJRSNPZMHMKpiEapamAJ2toMh 4yBz8DkuZMAAyEcyK670n/cJs4uLYMdWGv9+CHvHIOxA4UGDEAP3F61C6UNcMyjGmvIvWx BvCX/CqFamE8DSCz3XgW1l3oLvZ0TX/jkwrBiXVE3cDgNrzcmKRejf+AfY5ntA== Date: Mon, 23 Sep 2024 14:51:08 +0200 From: Erik Wienhold To: Asad Ali Cc: Dominique Devienne , pgsql-general@postgresql.org Subject: Re: Customize psql prompt to show current_role Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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 > 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