ALTER DEFAULT PRIVILEGES
[ FOR { ROLE | USER } target_role [, ...] ]
[ IN SCHEMA schema_name [, ...] ]
abbreviated_grant_or_revoke
where abbreviated_grant_or_revoke is one of:
GRANT { { SELECT | INSERT | UPDATE | DELETE | TRUNCATE | REFERENCES | TRIGGER }
[, ...] | ALL [ PRIVILEGES ] }
ON TABLES
TO { [ GROUP ] role_name | PUBLIC } [, ...] [ WITH GRANT OPTION ]
GRANT { { USAGE | SELECT | UPDATE }
[, ...] | ALL [ PRIVILEGES ] }
ON SEQUENCES
TO { [ GROUP ] role_name | PUBLIC } [, ...] [ WITH GRANT OPTION ]
GRANT { EXECUTE | ALL [ PRIVILEGES ] }
ON FUNCTIONS
TO { [ GROUP ] role_name | PUBLIC } [, ...] [ WITH GRANT OPTION ]
GRANT { USAGE | ALL [ PRIVILEGES ] }
ON TYPES
TO { [ GROUP ] role_name | PUBLIC } [, ...] [ WITH GRANT OPTION ]
REVOKE [ GRANT OPTION FOR ]
{ { SELECT | INSERT | UPDATE | DELETE | TRUNCATE | REFERENCES | TRIGGER }
[, ...] | ALL [ PRIVILEGES ] }
ON TABLES
FROM { [ GROUP ] role_name | PUBLIC } [, ...]
[ CASCADE | RESTRICT ]
REVOKE [ GRANT OPTION FOR ]
{ { USAGE | SELECT | UPDATE }
[, ...] | ALL [ PRIVILEGES ] }
ON SEQUENCES
FROM { [ GROUP ] role_name | PUBLIC } [, ...]
[ CASCADE | RESTRICT ]
REVOKE [ GRANT OPTION FOR ]
{ EXECUTE | ALL [ PRIVILEGES ] }
ON FUNCTIONS
FROM { [ GROUP ] role_name | PUBLIC } [, ...]
[ CASCADE | RESTRICT ]
REVOKE [ GRANT OPTION FOR ]
{ USAGE | ALL [ PRIVILEGES ] }
ON TYPES
FROM { [ GROUP ] role_name | PUBLIC } [, ...]
[ CASCADE | RESTRICT ]
ALTER DEFAULT PRIVILEGES 允许你设置应用到以后创建的对象的权限. (这不影响已分配给已经存在对象的权限.) 目前, 只有表(包括视图和外部表)、序列、函数 和类型(包括域)的权限能够被更改.
你能修改那些会被你自己或那些你是其中一员的角色所创建的对象的默认权限. 这个权限能被全局设置 (比如:给所有在当前数据库创建的对象), 或者只是给在指定模式中创建的对象. 默认权限规定每个模式的都会被加上,无论全局默认权限是不是为了特殊的对象类型.
根据 GRANT下的解释, 任何对象类型的默认权限通常授权全部可授与权限给对象所有者 , 以及授权一些权限给 PUBLIC . 不管怎样 , 改行为能通过使用ALTER DEFAULT PRIVILEGES更改全局默认权限来修改.
使用psql's \ddp 命令 来获取已存在的默认权限的分配信息. 权限制的含义与GRANT下\dp的解释相同。
如果你希望删除一个默认权限已经被修改的角色, 对这个角色来说,撤销默认权限上的修改 或者使用 DROP OWNED BY 来摆脱该角色的默认权限默认权限条目,这是必要的.
给你后来在模式myschema里创建的所有表(和视图)授予SELECT 权限 , 并且允许角色 webuser对他们执行INSERT:
ALTER DEFAULT PRIVILEGES IN SCHEMA myschema GRANT SELECT ON TABLES TO PUBLIC; ALTER DEFAULT PRIVILEGES IN SCHEMA myschema GRANT INSERT ON TABLES TO webuser;
撤销上面的操作, 因此后来创建的表不会拥有比常规情况还多的权限 :
ALTER DEFAULT PRIVILEGES IN SCHEMA myschema REVOKE SELECT ON TABLES FROM PUBLIC; ALTER DEFAULT PRIVILEGES IN SCHEMA myschema REVOKE INSERT ON TABLES FROM webuser;
为角色admin之后创建的所有的函数,移除那些通常授权在函数上的公共EXECUTE 权限:
ALTER DEFAULT PRIVILEGES FOR ROLE admin REVOKE EXECUTE ON FUNCTIONS FROM PUBLIC;