postgresql 如何使用postgresql中的选择获取当前数据库和用户名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26277356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:38:43  来源:igfitidea点击:

how to get current database and user name using a select in postgresql

sqlpostgresql

提问by vyegorov

I am using PostgreSQL 9.2, Is there any way to get an output like below using a selectquery ?

我正在使用PostgreSQL 9.2,有没有办法使用选择查询获得如下输出?

Database : mydb, User : myUser

数据库:mydb,用户:myUser

回答by Vivek S.

By using the inbuilt System Information Functions

通过使用内置的系统信息功能

1.) Currently using database

1.) 当前使用数据库

   select current_database()

2.) Connected User

2.) 连接用户

  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

Live Demo

回答by vyegorov

Check this part of the manualfor more functions.

查看手册的这部分以了解更多功能。

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