postgresql 如何在 psql 上检查连接的用户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39735141/
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-09-09 06:18:56  来源:igfitidea点击:

How to check connected user on psql

postgresql

提问by Alexander Rumanovsk

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

在我的 PostgreSQL 数据库中,我有 2 个用户:postgres 和 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?

默认用户是 postgres,但该用户无权查询我的外部表,而 myuser 有。如何检查我是否与正确的用户建立了联系?

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

如果我使用了错误的用户,如何更改为正确的用户?

回答by Michas

To get information about current connection from psql comman prompt:

从 psql 命令提示符获取有关当前连接的信息:

\conninfo

This display more informations, though.

不过,这会显示更多信息。

To change user:

更改用户:

\c - a_new_user

The ‘-' is substitute for current database. You can write this to change database and user:

'-' 代替当前数据库。您可以编写此代码来更改数据库和用户:

\c a_new_database a_new_user

Using SQL to get information:

使用SQL获取信息:

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

此页面列出了一些有趣的函数和变量。
https://www.postgresql.org/docs/current/static/functions-info.html

回答by wjv

To view your current authorisation:

查看您当前的授权:

SELECT session_user, current_user;

To change to another role to which you've been granted access:

要更改为您被授予访问权限的另一个角色:

SET ROLE <role_name>;

For instance, if you've granted the postgresrole to myuserwith…

例如,如果您已将postgres角色授予myuser...

GRANT postgres TO myuser;

… then myusercan temporarily “su” to postgreswith:

...然后myuser可以暂时“su”到postgres

SET ROLE postgres;

Note (1):Superusers can SET ROLEto any other role without explicitly being granted access to it.

注意 (1):超级用户可以SET ROLE在没有明确授予访问权限的情况下担任任何其他角色。

Note (2):Using SET ROLEchanges only current_userand not session_user.

注 (2):SET ROLE仅使用更改current_user而不使用session_user.

To return to your default (session) role:

要返回您的默认(会话)角色:

RESET ROLE;

If you're logged in as a superuser, you can change the authorisation profile for the entire session with:

如果您以超级用户身份登录,则可以使用以下命令更改整个会话的授权配置文件:

SET SESSION AUTHORIZATION <role_name>;

This changes both current_userand session_user.

这会改变current_usersession_user

Postgres authorisation is a complicated (arguably over-complicated) topic. I suggest reading through the official documentation.

Postgres 授权是一个复杂(可以说是过于复杂)的话题。我建议通读官方文档。

Edit:To make things simpler, I define the following alias in my ~/.psqlrc:

编辑:为了使事情更简单,我在我的中定义了以下别名~/.psqlrc

\set whoami 'SELECT session_user, current_user, :''HOST'' host, :''PORT'' port, :''DBNAME'' dbname;'

Now I can simply type :whoamiat the psql prompt to see my authorisation details.

现在我可以简单地:whoami在 psql 提示符下键入以查看我的授权详细信息。

回答by stdunbar

You can always run the query "select current_user" to determine who you are now. As far as being the correct user, what database client are you using? You would normally specify that information when you connect to the database.

您始终可以运行查询“select current_user”来确定您现在的身份。至于是正确的用户,您使用的是什么数据库客户端?您通常会在连接到数据库时指定该信息。