PostgreSQL 权限信息表information_schema.table_privileges

前端之家收集整理的这篇文章主要介绍了PostgreSQL 权限信息表information_schema.table_privileges前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

information_schema.table_privileges表记录着所有用户的权限信息。

  1. postgres=# \d+ information_schema.table_privileges
  2. View "information_schema.table_privileges"
  3. Column | Type | Modifiers | Storage | Description
  4. ----------------+-----------------------------------+-----------+----------+-------------
  5. grantor | information_schema.sql_identifier | | extended | 授权者
  6. grantee | information_schema.sql_identifier | | extended | 被授权者
  7. table_catalog | information_schema.sql_identifier | | extended | 数据库
  8. table_schema | information_schema.sql_identifier | | extended | schema
  9. table_name | information_schema.sql_identifier | | extended | 表名
  10. privilege_type | information_schema.character_data | | extended | 对表的操作权限
  11. is_grantable | information_schema.yes_or_no | | extended |
  12. with_hierarchy | information_schema.yes_or_no | | extended |
  13. View definition:
  14. SELECT u_grantor.rolname::information_schema.sql_identifier AS grantor,grantee.rolname::information_schema.sql_identifier AS grantee,current_database()::information_schema.sql_identifier AS table_catalog,nc.nspname::information_schema.sql_identifier AS table_schema,c.relname::information_schema.sql_identifier AS table_name,c.prtype::information_schema.character_data AS privilege_type,CASE WHEN pg_has_role(grantee.oid,c.relowner,'USAGE'::text) OR c.grantable THEN 'YES'::text ELSE 'NO'::text END::information_schema.yes_or_no AS is_grantable,CASE WHEN c.prtype = 'SELECT'::text THEN 'YES'::text ELSE 'NO'::text END::information_schema.yes_or_no AS with_hierarchy FROM ( SELECT pg_class.oid,pg_class.relname,pg_class.relnamespace,pg_class.relkind,pg_class.relowner,(aclexplode(COALESCE(pg_class.relacl,acldefault('r'::"char",pg_class.relowner)))).grantor AS grantor,pg_class.relowner)))).grantee AS grantee,pg_class.relowner)))).privilege_type AS privilege_type,pg_class.relowner)))).is_grantable AS is_grantable FROM pg_class) c(oid,relname,relnamespace,relkind,relowner,grantor,grantee,prtype,grantable),pg_namespace nc,pg_authid u_grantor,( SELECT pg_authid.oid,pg_authid.rolname FROM pg_authid UNION ALL SELECT 0::oid AS oid,'PUBLIC'::name) grantee(oid,rolname) WHERE c.relnamespace = nc.oid AND (c.relkind = ANY (ARRAY['r'::"char",'v'::"char"])) AND c.grantee = grantee.oid AND c.grantor = u_grantor.oid AND (c.prtype = ANY (ARRAY['INSERT'::text,'SELECT'::text,'UPDATE'::text,'DELETE'::text,'TRUNCATE'::text,'REFERENCES'::text,'TRIGGER'::text])) AND (pg_has_role(u_grantor.oid,'USAGE'::text) OR pg_has_role(grantee.oid,'USAGE'::text) OR grantee.rolname = 'PUBLIC'::name);

现在创建一个角色并赋予SELECT权限,来观察该系统表的数据。


1. 创建角色

  1. postgres=# create role john login NOSUPERUSER NOCREATEDB NOCREATEROLE NOINHERIT;
  2. CREATE ROLE

2. 赋予该角色一个数据库的表的SEKECT权限

  1. -- 赋予数据库链接权限
  2. postgres=# GRANT CONNECT ON DATABASE postgres TO john;
  3. GRANT
  1. -- 赋予表的查询功能
  2. postgres=# GRANT SELECT ON ALL TABLES IN SCHEMA public TO john;
  3. GRANT
  1. -- tb1表的INSERT INTO权限
  2. postgres=# GRANT INSERT ON tb1 TO john;
  3. GRANT

3. 查看该角色的所有权限

  1. postgres=# SELECT * from information_schema.table_privileges where grantee='john' order by privilege_type;
  2. grantor | grantee | table_catalog | table_schema | table_name | privilege_type | is_grantable | with_hierarchy
  3. ----------+---------+---------------+--------------+----------------------+----------------+--------------+----------------
  4. postgres | john | postgres | public | tb1 | INSERT | NO | NO postgres | john | postgres | public | book | SELECT | NO | YES postgres | john | postgres | public | weather | SELECT | NO | YES postgres | john | postgres | public | cities | SELECT | NO | YES postgres | john | postgres | public | tb1 | SELECT | NO | YES postgres | john | postgres | public | book2 | SELECT | NO | YES postgres | john | postgres | public | person | SELECT | NO | YES postgres | john | postgres | public | tb2 | SELECT | NO | YES postgres | john | postgres | public | orders | SELECT | NO | YES postgres | john | postgres | public | test_unlogged | SELECT | NO | YES postgres | john | postgres | public | test | SELECT | NO | YES postgres | john | postgres | public | system_monitor | SELECT | NO | YES postgres | john | postgres | public | tb3 | SELECT | NO | YES postgres | john | postgres | public | pg_stat_statements | SELECT | NO | YES postgres | john | postgres | public | view_business_device | SELECT | NO | YES postgres | john | postgres | public | student | SELECT | NO | YES postgres | john | postgres | public | pgbench_tellers | SELECT | NO | YES postgres | john | postgres | public | pgbench_branches | SELECT | NO | YES postgres | john | postgres | public | pgbench_accounts | SELECT | NO | YES postgres | john | postgres | public | pgbench_history | SELECT | NO | YES postgres | john | postgres | public | goods | SELECT | NO | YES postgres | john | postgres | public | bloat | SELECT | NO | YES (22 rows)

猜你在找的Postgre SQL相关文章