PostgreSQL:显示具体用户的所有权限

jaql4c8m  于 2023-04-29  发布在  PostgreSQL
关注(0)|答案(3)|浏览(174)

如何查询Postgres数据字典以找出特定用户拥有的所有权限。
我一直在寻找一个解决方案,我找不到任何东西。谢谢,祝你愉快

nbysray5

nbysray51#

表权限:

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';
5lwkijsr

5lwkijsr2#

这个命令对我很有帮助:

\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: List the database privileges using psql

就这样

希望这能帮上忙

u5rb5r59

u5rb5r593#

这是对我最有效的方法。短而干净。
\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                          | {}        |

相关问题