PostgreSQL:显示特定用户的所有权限

62
如何查询Postgres数据字典以查找特定用户拥有的所有权限。
我一直在寻找解决方案,但是找不到任何内容。谢谢,祝愉快的一天。
4个回答

109
表权限:
SELECT *
  FROM information_schema.role_table_grants 
 WHERE grantee = 'YOUR_USER';

所有权:

SELECT *
  FROM pg_tables 
 WHERE tableowner = 'YOUR_USER';

架构权限:

      SELECT r.usename AS grantor,
             e.usename AS grantee,
             nspname,
             privilege_type,
             is_grantable
        FROM pg_namespace
JOIN LATERAL (SELECT *
                FROM aclexplode(nspacl) AS x) a
          ON true
        JOIN pg_user e
          ON a.grantee = e.usesysid
        JOIN pg_user r
          ON a.grantor = r.usesysid 
       WHERE e.usename = 'YOUR_USER';

1
很棒的解决方案,但对于模式权限,您不需要使用连接侧。只需使用 ... FROM pg_namespace, aclexplode(nspacl) AS a ... 即可。 - Jeff
11
不确定这个功能是什么,但所有查询结果都为空。然而,我的用户可以访问他们自己的数据库、表,甚至可以查看外部数据库并在那里创建/删除新表!这个答案没有帮助。 - ygoe
3
@ygoe:如果用户拥有一个表(或数据库),则不存在特殊授权,因为对象的所有者始终具有对该对象的完全访问权限(无需授权)。 - user330315
注意:JOIN LATERAL 在 Postgres 9.3 版本中可用,因此上面的 schema permissions 查询在 AWS Redshift 中不起作用。 - Gergely M
1
这对于数据库对象来说似乎都很好。那么,如何找到角色对数据库本身的现有权限呢?谢谢。 - rik
Jeff,您介意详细说明一下SCHEMA权限的显示吗? - vrms

7

这是我认为最好的方法,简短而干净。

\du 列出所有用户账户和角色,\du+ 是扩展版本,显示更多信息。

# \du
                                        List of roles
     Role name      |                         Attributes                         | Member of
--------------------+------------------------------------------------------------+-----------
 padmin             | Superuser, Create role, Create DB                          | {}
 test               |                                                            | {}
 postgres           | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 root               | Superuser, Create role, Create DB                          | {}

# \du+
                                               List of roles
     Role name      |                         Attributes                         | Member of | Description
--------------------+------------------------------------------------------------+-----------+-------------
 padmin             | Superuser, Create role, Create DB                          | {}        |
 test               |                                                            | {}        |
 postgres           | Superuser, Create role, Create DB, Replication, Bypass RLS | {}        |
 root               | Superuser, Create role, Create DB                          | {}        |

6
这个命令对我很有帮助:

这个命令对我很有帮助:

\l

这是我如何使用它的方法:

postgres=# \l

                        List of databases
 Name   | Owner    | Encoding | Collate | Ctype |          Access privileges          
------------------------------+-----------------+----------+---------+-------+-------------------------------------
 mydb1  | postgres | UTF8     | en_NG   | en_NG | =Tc/postgres                       +
        |          |          |         |       | postgres=CTc/postgres              +
        |          |          |         |       | myuser=CTc/postgres
 mydb2  | postgres | UTF8     | en_NG   | en_NG | =Tc/postgres                       +
        |          |          |         |       | postgres=CTc/postgres              +
        |          |          |         |       | my_user=CTc/postgres

资源: PostgreSQL: 使用psql列出数据库权限

就这些了。

希望对您有所帮助。


0

您也可以使用这个方法来查看用户是否拥有除了SELECT之外的其他权限

SELECT * FROM information_schema.role_table_grants WHERE grantee = 'username' AND with_hierarchy = 'YES'


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接