Postgresql – How to check connected user on psql

postgresql

In my PostgreSQL database I have 2 users: postgres and myuser.

The default user is postgres, but this user has no permission to query my foreign tables and myuser does. How can I check if I'm connected with the right user?

If I'm using the wrong user, how do I change to the right one?

Best Answer

To get information about current connection from the psql command prompt:

\conninfo

This displays more informations, though.

To change user:

\c - a_new_user

‘-’ substitutes for the current database.

To change database and user:

\c a_new_database a_new_user

The SQL command to get this information:

SELECT current_user;

Examples:

postgres=# \conninfo
You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5432"

postgres=# \c a_new_database a_new_user
psql (12.1 (Ubuntu 12.1-1.pgdg16.04+1), server 9.5.20)
You are now connected to database "a_new_database" as user "a_new_user".

a_new_database=# SELECT current_user;
 current_user 
--------------
 a_new_user
(1 row)


This page list few interesting functions and variables.
https://www.postgresql.org/docs/current/static/functions-info.html