pg_permissions
查看对象权限并将其与期望状态进行比较
仓库
cybertec-postgresql/pg_permissions
https://github.com/cybertec-postgresql/pg_permissions
源码
pg_permissions-REL_1_3.tar.gz
pg_permissions-REL_1_3.tar.gz
概览
| 扩展包名 | 版本 | 分类 | 许可证 | 语言 |
|---|---|---|---|---|
pg_permissions | 1.4 | ADMIN | BSD 2-Clause | SQL |
| ID | 扩展名 | Bin | Lib | Load | Create | Trust | Reloc | 模式 |
|---|---|---|---|---|---|---|---|---|
| 5140 | pg_permissions | 否 | 是 | 否 | 是 | 否 | 否 | - |
| 相关扩展 | pg_readonly pgaudit set_user pg_upless safeupdate pgauditlogtofile credcheck login_hook |
|---|
版本
| 类型 | 仓库 | 版本 | PG 大版本 | 包名 | 依赖 |
|---|---|---|---|---|---|
| EXT | MIXED | 1.4 | 1817161514 | pg_permissions | - |
| RPM | PGDG | 1.4 | 1817161514 | pg_permissions_$v | - |
| DEB | PIGSTY | 1.4 | 1817161514 | postgresql-$v-pg-permissions | - |
构建
您可以使用 pig build 命令构建 pg_permissions 扩展的 DEB 包:
pig build pkg pg_permissions # 构建 DEB 包
安装
您可以直接安装 pg_permissions 扩展包的预置二进制包,首先确保 PGDG 和 PIGSTY 仓库已经添加并启用:
pig repo add pgsql -u # 添加仓库并更新缓存
使用 pig 或者是 apt/yum/dnf 安装扩展:
pig install pg_permissions; # 当前活跃 PG 版本安装
pig ext install -y pg_permissions -v 18 # PG 18
pig ext install -y pg_permissions -v 17 # PG 17
pig ext install -y pg_permissions -v 16 # PG 16
pig ext install -y pg_permissions -v 15 # PG 15
pig ext install -y pg_permissions -v 14 # PG 14
dnf install -y pg_permissions_18 # PG 18
dnf install -y pg_permissions_17 # PG 17
dnf install -y pg_permissions_16 # PG 16
dnf install -y pg_permissions_15 # PG 15
dnf install -y pg_permissions_14 # PG 14
apt install -y postgresql-18-pg-permissions # PG 18
apt install -y postgresql-17-pg-permissions # PG 17
apt install -y postgresql-16-pg-permissions # PG 16
apt install -y postgresql-15-pg-permissions # PG 15
apt install -y postgresql-14-pg-permissions # PG 14
创建扩展:
CREATE EXTENSION pg_permissions;
用法
pg_permissions 让你查看数据库对象的实际权限,并与期望的权限状态进行比较。
定义期望权限
向 permission_target 插入条目以描述应有的权限:
INSERT INTO permission_target (role_name, permissions, object_type, schema_name)
VALUES ('appuser', '{SELECT,INSERT,UPDATE,DELETE}', 'TABLE', 'appschema');
INSERT INTO permission_target (role_name, permissions, object_type, schema_name)
VALUES ('appuser', '{USAGE}', 'SCHEMA', 'appschema');
INSERT INTO permission_target (role_name, permissions, object_type, schema_name, object_name)
VALUES ('appuser', '{USAGE}', 'SEQUENCE', 'appschema', 'appseq');
将 object_name 或 column_name 设置为 NULL 可应用于模式中该类型的所有对象。
查找权限差异
SELECT * FROM permission_diffs();
返回 missing = TRUE(权限应存在但不存在)或 missing = FALSE(不应存在的多余权限)的行。
查看实际权限
可用视图(所有视图具有相同的列结构):
database_permissions– 当前数据库的权限schema_permissions– 模式的权限table_permissions– 表的权限view_permissions– 视图的权限column_permissions– 表/视图列的权限function_permissions– 函数的权限sequence_permissions– 序列的权限all_permissions– 以上所有的 UNION
SELECT * FROM table_permissions WHERE role_name = 'appuser' AND schema_name = 'appschema';
通过视图 Grant/Revoke
权限视图的 granted 列是可更新的——更新它会执行相应的 GRANT 或 REVOKE 命令。
注意:超级用户不显示在视图中(他们自动拥有所有权限)。