Use*_*sbs 11 postgresql user-roles
我需要一个新用户,但应该授予其他现有用户/角色所具有的所有权限.
例如
如果创建了新用户B,我需要相同的权限,
不要问为什么:/
实际上,用户A对不同的表,模式和函数具有自定义权限; 所以手动授予新用户权限是一个非常繁琐冗长的过程.任何帮助都会很好.
尝试类似的东西:
GRANT A TO B;
Run Code Online (Sandbox Code Playgroud)
它将授予角色A到B的所有权利.
有关详细信息,请阅读this本手册的章节
首先要了解的是roles和users同一件事。实际上,没有一个叫做usertrue 的东西,只是一个ROLE带有LOGIN选项的东西。
第二roles可以授予他人roles。
角色的第三项特权可以被继承。
因此,假设您以如下方式创建了用户a:
CREATE ROLE A LOGIN;
GRANT SELECT ON table1 TO a;
GRANT EXECUTE ON FUNCTION xxx TO a;
Run Code Online (Sandbox Code Playgroud)
您应该能够创建与第一个角色类似的第二个角色,例如:
CREATE ROLE b LOGIN;
GRANT a TO b;
Run Code Online (Sandbox Code Playgroud)
我必须编写 pgpsql 代码来循环遍历用户 A 的权限并将其授予用户 B。这一切都没有任何问题。
create or replace function update_user_privileges() returns text as
$$
declare
info record;
str text;
begin
/*Grant privileges to user B the same as with user A for a given table schema*/
str:='';
FOR info IN
select * from information_schema.table_privileges where table_schema='public' and grantee = 'A'
LOOP
/*append the tables' name, for which we are assigning privileges from user A to B*/
str:= str ||info.table_name || ',';
/*this is the main statement to grant any privilege*/
execute 'GRANT '|| info.privilege_type ||' on table public.'|| info.table_name || ' to B';
END LOOP;
return str;
end
$$ language 'plpgsql';
Run Code Online (Sandbox Code Playgroud)
用法:复制/粘贴此代码以创建此函数,然后执行
select update_user_privileges();
Run Code Online (Sandbox Code Playgroud)
**您必须根据您的表架构和表名称调整它。希望它对任何人都有帮助
| 归档时间: |
|
| 查看次数: |
13415 次 |
| 最近记录: |