简体   繁体   English

如何在PostgreSQL中使用`select`获取当前数据库和用户名?

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

I'm using PostgreSQL 9.2 .我正在使用PostgreSQL 9.2

Is there any way to get an output like,有什么办法可以得到像,

Database : mydb, User : myUser数据库:mydb,用户:myUser

using a select query?使用选择查询?

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

Check this part of the manual for more functions.查看手册的这一部分以获取更多功能。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM