gpt4 book ai didi

sql - 选择 current_user 的组名

转载 作者:行者123 更新时间:2023-11-29 12:16:21 27 4
gpt4 key购买 nike

我使用以下命令检查 PostgreSQL 数据库中用户的组或角色,

select rolname from pg_authid where pg_has_role('some_username', rolname, 'member');

如上所示,我必须在上面的查询中手动输入用户名,如上面的 'some_username'

有没有办法查询 current_user,我指的是当前连接到数据库的用户。类似下面的命令,

select rolname from pg_authid where pg_has_role(current_user, rolname, 'member'); 

这个命令不能正常工作。它从 pg_authid 返回完整的 rolname 列。

最佳答案

要访问 pg_authid,您需要是 super 用户,因此 current_user 将是 super 用户,并且拥有所有角色。

要在您以非 super 用户身份登录时获取该信息,请使用 pg_roles

Quote from the manual for pg_roles

This is simply a publicly readable view of pg_authid that blanks out the password field.

select rolname 
from pg_roles
where pg_has_role(current_user, rolname, 'member');

关于sql - 选择 current_user 的组名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52076227/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com