Cột relacl của danh mục hệ thống pg_class chứa tất cả thông tin về các đặc quyền.
Dữ liệu mẫu trong giản đồ public thuộc sở hữu của postgres với các khoản tài trợ cho newuser :
create table test(id int);
create view test_view as select * from test;
grant select, insert, update on test to newuser;
grant select on test_view to newuser;
Truy vấn pg_class :
select
relname,
relkind,
coalesce(nullif(s[1], ''), 'public') as grantee,
s[2] as privileges
from
pg_class c
join pg_namespace n on n.oid = relnamespace
join pg_roles r on r.oid = relowner,
unnest(coalesce(relacl::text[], format('{%s=arwdDxt/%s}', rolname, rolname)::text[])) acl,
regexp_split_to_array(acl, '=|/') s
where nspname = 'public'
and relname like 'test%';
relname | relkind | grantee | privileges
-----------+---------+----------+------------
test | r | postgres | arwdDxt <- owner postgres has all privileges on the table
test | r | newuser | arw <- newuser has append/read/write privileges
test_view | v | postgres | arwdDxt <- owner postgres has all privileges on the view
test_view | v | newuser | r <- newuser has read privilege
(4 rows)
Nhận xét:
-
coalesce(relacl::text[], format('{%s=arwdDxt/%s}', rolname, rolname))- Không có trongrelaclcó nghĩa là chủ sở hữu có tất cả các đặc quyền; -
unnest(...) acl-relacllà một mảng củaaclitem, một phần tử mảng cho một người dùng; -
regexp_split_to_array(acl, '=|/') s- táchaclitemthành:tên người dùng [1], đặc quyền của [2]; -
coalesce(nullif(s[1], ''), 'public') as grantee- tên người dùng trống có nghĩa làpublic.
Sửa đổi truy vấn để chọn người dùng cá nhân hoặc loại quan hệ cụ thể hoặc một lược đồ khác, v.v. ...
Đọc trong tài liệu:
- Danh mục
pg_class, -
GRANTvới mô tả của hệ thống acl.
Theo cách tương tự, bạn có thể nhận thông tin về các đặc quyền được cấp trên lược đồ (cột nspacl trong pg_namespace
) và cơ sở dữ liệu ( datacl trong pg_database
)