本文记述在postgreSQL中对用户/角色操作库、模式、表、序列、函数、存储过程的权限管理
针对数据库的授权
授权:grant 权限 on database 数据库 to 用户/角色;
撤权:revoke 权限 on database 数据库 from 用户/角色;
针对模式的授权
授权:grant 权限 on schema 模式 to 用户/角色; – 针对单模式授权
撤权:revoke 权限 on schema 模式 from 用户/角色; – 针对单模式撤权
针对表的授权
授权:grant 权限 on 模式.表 to 用户/角色; – 针对选定表,如grant select on public.order, grade to admin
grant 权限 on all tables in schema 模式 to 用户/角色; – 针对全部表
撤权:revoke 权限 on all tables in schema 模式 from 用户/角色; – 针对全部表
revoke 权限 on 模式.表 from 用户/角色; – 针对选定表
针对序列的授权
授权:grant 权限 on sequence 序列 to 用户/角色; – 单个序列授权,一般是all权限
grant 权限 on all sequences in schema 模式 to 用户/角色; – 批量序列授权,一般是all权限
撤权:revoke 权限 on sequence 序列 from 用户/角色; – 单个序列撤权,一般是all权限
revoke 权限 on all sequences in schema 模式 to 用户/角色; – 批量序列撤权,一般是all权限
针对函数授权
授权:grant 权限 on function 函数名(参数类型列表) to 用户/角色; – 单个函数授权,一般是execute权限
grant 权限 on all functions in schema 模式 to 用户/角色; – 批量函数授权,一般是execute权限
撤权:revoke 权限 on function 函数名(参数类型列表) from 用户/角色; – 单个函数撤权
revoke 权限 on all functions in schema 模式 from 用户/角色; – 批量函数撤权
针对存储过程授权
授权:grant 权限 on procedure 存储过程名(参数类型列表) to 用户/角色; – 单个存储过程授权,一般是execute权限
grant 权限 on all procedures in schema 模式 from 用户/角色; – 批量存储过程授权,一般是execute权限
撤权:revoke 权限 on procedure 存储过程名(参数类型列表) from 用户/角色; – 单个存储过程撤权,一般是execute权限
revoke 权限 on all procedures in schema 模式 from 用户/角色; – 批量存储过程撤权
权限
表table权限
- select:查询表中数据权限
- insert:向表中插入数据权限
- update:修改表中数据权限(需先有 select 权限)
- delete:删除表中数据权限
- truncate:清空全表权限(比 delete 更高效,但不可回滚)
- references:创建指向该表的外键约束权限(用于关联其他表)
- trigger:在表上创建或启用触发器权限
- all privileges:上述权限集合
模式schema权限
- create 在模式下创建对象(表,视图,自定义函数,触发器等)的权限
- usage 访问模式内的对象权限
- all privileges 上述权限集合
数据库database权限
- create 在数据库中创建新的模式权限
- connect 连接数据库权限
- temporary/temp 创建临时表权限
- all privileges 上述权限集合
序列权限
- usage 允许使用nextval()和setval()函数
- select 允许查询序列当前值currval()函数
- update 允许修改序列属性(如重置起始值)
- all 上述权限集合
函数权限
- execute 执行权限,允许用户调用并执行函数
- create 创建权限,允许用户创建函数
- alter 修改权限,允许用户修改函数
- drop 删除权限,允许用户删除函数
- debug 调试权限,允许用户查看存储过程的源代码,但不能执行或修改
- references 引用权限,允许在函数中引用其他对象(如跨表查询),但非直接执行权
存储过程权限
- execute 执行权限,允许用户调用并执行存储过程
- create 创建权限,允许用户创建新的存储过程
- alter 修改权限,允许用户修改存储过程
- drop 删除权限,允许用户删除存储过程
- debug 调试权限,允许用户查看存储过程的源代码,但不能执行或修改
- usage 引用权限,允许用户引用存储过程(如在其他过程中调用),但不包含执行权