创建具有不同权限的用户并在PostgreSQL中列出这些用户及其权限

t98cgbkg  于 2023-04-20  发布在  PostgreSQL
关注(0)|答案(1)|浏览(138)

我想创建具有不同权限的用户,例如只为数据库创建读写和管理权限。我使用赠款创建用户,但当我使用\du时,它没有显示任何可以用来查找用户权限的内容。我也使用了这些命令,但它们没有显示任何可以用来查找用户权限的内容。

SELECT rolname, rolsuper, rolinherit, rolcreaterole, rolcreatedb, rolcanlogin, rolreplication, rolbypassrls
FROM pg_roles
WHERE rolname = 'your_username';

SELECT grantee, privilege_type
FROM information_schema.table_privileges
WHERE table_schema = 'your_schema' AND table_name = 'your_table';

我也使用了像pg_read_all_data这样的预定义角色,但它会影响所有数据库,但我希望它会影响特定的数据库。
我该怎么办?

eblbsuwk

eblbsuwk1#

我使用这些语句创建用户:
这将阻止默认用户创建表:

REVOKE CREATE ON SCHEMA public FROM public;

Read Only

CREATE USER read WITH PASSWORD 'Read@123';GRANT USAGE ON SCHEMA public TO read;GRANT SELECT ON ALL TABLES IN SCHEMA public TO read;ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO read;

Read & Write

CREATE USER write WITH PASSWORD 'Write@123'; GRANT USAGE ON SCHEMA public TO write; GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO write; ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO write;

Admin

CREATE USER admin WITH PASSWORD 'Admin@123';GRANT USAGE,CREATE ON SCHEMA public TO admin;GRANT ALL ON ALL TABLES IN SCHEMA public TO admin;ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO admin;

然后我使用这个查询来查找previleges:

select grantee, privilege_type from information_schema.role_table_grants;

SELECT * FROM information_schema. table_privileges;

SELECT "grantee",
array_to_string(array_agg(distinct "privilege_type"),',') AS privilege_type
FROM information_schema.role_table_grants
where grantee not in ('postgres','PUBLIC')
GROUP BY "grantee";

\z

SELECT 
  nspname,         -- schema name
  defaclobjtype,   -- object type
  defaclacl        -- default access privileges
FROM pg_default_acl a JOIN pg_namespace b ON a.defaclnamespace=b.oid;

结果应该是这样的:

相关问题