How to list current user and database in PSQL

Started by peterlenover 12 years ago6 messagesgeneral
Jump to latest
#1peterlen
peteralen@earthlink.net

After logging into the PSQL Shell Script as a particular user and database, I
want to display who my current user is and what database I am in as I may
have forgotten who I am logged in as in order to make sure any commands are
going to the right place. In Oracle's SQLPlus you can do something like
"show user" and get feedback on the user you are logged in as. Is there an
equivalent thing for PSQL to list the user and database?

Thanks - Peter

--
View this message in context: http://postgresql.1045698.n5.nabble.com/How-to-list-current-user-and-database-in-PSQL-tp5784928.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#2Adrian Klaver
adrian.klaver@aklaver.com
In reply to: peterlen (#1)
Re: How to list current user and database in PSQL

On 12/31/2013 10:55 AM, peterlen wrote:

After logging into the PSQL Shell Script as a particular user and database, I
want to display who my current user is and what database I am in as I may
have forgotten who I am logged in as in order to make sure any commands are
going to the right place. In Oracle's SQLPlus you can do something like
"show user" and get feedback on the user you are logged in as. Is there an
equivalent thing for PSQL to list the user and database?

test=> select current_user;
current_user
--------------
aklaver
(1 row)

test=> select current_database();
current_database
------------------
test

Or you can change the psql prompt to display the information:

http://www.postgresql.org/docs/9.3/interactive/app-psql.html

Search for:

"Prompting"

Details the variables available

"Files"

Explains how to persist this information in a psqlrc file.

Thanks - Peter

--
View this message in context: http://postgresql.1045698.n5.nabble.com/How-to-list-current-user-and-database-in-PSQL-tp5784928.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.

--
Adrian Klaver
adrian.klaver@gmail.com

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#3Bosco Rama
postgres@boscorama.com
In reply to: peterlen (#1)
Re: How to list current user and database in PSQL

On 12/31/13 10:55, peterlen wrote:

After logging into the PSQL Shell Script as a particular user and database, I
want to display who my current user is and what database I am in as I may
have forgotten who I am logged in as in order to make sure any commands are
going to the right place. In Oracle's SQLPlus you can do something like
"show user" and get feedback on the user you are logged in as. Is there an
equivalent thing for PSQL to list the user and database?

Use the connect meta without arguments, i.e.

xyz> \c
You are now connected to database "xyz" as user "demo".

or for something 'show' based:

xyz> show session authorization;
session_authorization
-----------------------
demo
(1 row)

HTH,
Bosco.

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#4Bosco Rama
postgres@boscorama.com
In reply to: Bosco Rama (#3)
Re: How to list current user and database in PSQL

On 12/31/13 11:06, Bosco Rama wrote:

Use the connect meta without arguments, i.e.

xyz> \c
You are now connected to database "xyz" as user "demo".

Hmmm. Scratch that. That actually does a reconnect. Probably not
what you want, especially is you are in the middle of a transaction. :-(

Bosco.

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#5peterlen
peteralen@earthlink.net
In reply to: Adrian Klaver (#2)
Re: How to list current user and database in PSQL

Excellent. That works. Many thanks!

--
View this message in context: http://postgresql.1045698.n5.nabble.com/How-to-list-current-user-and-database-in-PSQL-tp5784928p5784932.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#6bricklen
bricklen@gmail.com
In reply to: peterlen (#1)
Re: How to list current user and database in PSQL

On Tue, Dec 31, 2013 at 10:55 AM, peterlen <peteralen@earthlink.net> wrote:

After logging into the PSQL Shell Script as a particular user and
database, I
want to display who my current user is and what database I am in as I may
have forgotten who I am logged in as in order to make sure any commands are
going to the right place. In Oracle's SQLPlus you can do something like
"show user" and get feedback on the user you are logged in as. Is there an
equivalent thing for PSQL to list the user and database?

Thanks - Peter

As Adrian Klaver mentioned, you can set your psql prompt. Here are few
entries from my $HOME/.psqlrc

\set PROMPT1 '%n@%`hostname`:%> [%/] %x%# '
\timing on
\pset pager off
\x auto
\set HISTCONTROL = ignoreboth