How to get current database and user name with `select` in PostgreSQL?

By using the inbuilt System Information Functions

1.) Currently using database

   select current_database()

2.) Connected User

  select user

To get the desired output use either this

 select 'Database : ' ||current_database()||', '||'User : '|| user db_details

or

select format('Database: %s, User: %s',current_database(),user) db_details

Live Demo


Check this part of the manual for more functions.

SELECT current_user,
       user,
       session_user,
       current_database(),
       current_catalog,
       version();

Tags:

Sql

Postgresql