跳转到主要内容

pg_command_fw

PostgreSQL 的 DDL 与 utility 命令防火墙

pg_command_fw · PostgreSQL 的 DDL 与 utility 命令防火墙

概览

ID扩展软件包版本类别许可证编程语言
7400pg_command_fwpg_command_fw0.1.0SECBSD-3-ClauseRust
属性可执行文件动态库需要预加载需要建扩展可迁移可信
--sLd--否是是是否否
Requires shared_preload_libraries = pg_command_fw to activate hooks for all sessions.

软件包

类型仓库版本兼容的 PostgreSQL 大版本包名模式依赖
EXTPIGSTY0.1.019 18 17 16 15pg_command_fw-
RPMPIGSTY0.1.019 18 17 16 15pg_command_fw_$v-
DEBPIGSTY0.1.019 18 17 16 15postgresql-$v-pg-command-fw-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
el8.aarch64N/APIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
el9.x86_64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
el9.aarch64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
el10.x86_64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
el10.aarch64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
d12.x86_64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
d12.aarch64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
d13.x86_64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
d13.aarch64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u22.x86_64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u22.aarch64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u24.x86_64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u24.aarch64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u26.x86_64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u26.aarch64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
软件包版本平台仓库大小下载
pg_command_fw_190.1.0el9.x86_64pigsty890.0 KiBpg_command_fw_19-0.1.0-4PGSTY.el9.x86_64.rpm
pg_command_fw_190.1.0el9.aarch64pigsty859.5 KiBpg_command_fw_19-0.1.0-4PGSTY.el9.aarch64.rpm
pg_command_fw_190.1.0el10.x86_64pigsty890.5 KiBpg_command_fw_19-0.1.0-4PGSTY.el10.x86_64.rpm
pg_command_fw_190.1.0el10.aarch64pigsty859.6 KiBpg_command_fw_19-0.1.0-4PGSTY.el10.aarch64.rpm
postgresql-19-pg-command-fw0.1.0d12.x86_64pigsty599.0 KiBpostgresql-19-pg-command-fw_0.1.0-4PGSTY~bookworm_amd64.deb
postgresql-19-pg-command-fw0.1.0d12.aarch64pigsty522.5 KiBpostgresql-19-pg-command-fw_0.1.0-4PGSTY~bookworm_arm64.deb
postgresql-19-pg-command-fw0.1.0d13.x86_64pigsty599.6 KiBpostgresql-19-pg-command-fw_0.1.0-4PGSTY~trixie_amd64.deb
postgresql-19-pg-command-fw0.1.0d13.aarch64pigsty523.5 KiBpostgresql-19-pg-command-fw_0.1.0-4PGSTY~trixie_arm64.deb
postgresql-19-pg-command-fw0.1.0u22.x86_64pigsty666.3 KiBpostgresql-19-pg-command-fw_0.1.0-4PGSTY~jammy_amd64.deb
postgresql-19-pg-command-fw0.1.0u22.aarch64pigsty618.6 KiBpostgresql-19-pg-command-fw_0.1.0-4PGSTY~jammy_arm64.deb
postgresql-19-pg-command-fw0.1.0u24.x86_64pigsty658.6 KiBpostgresql-19-pg-command-fw_0.1.0-4PGSTY~noble_amd64.deb
postgresql-19-pg-command-fw0.1.0u24.aarch64pigsty611.9 KiBpostgresql-19-pg-command-fw_0.1.0-4PGSTY~noble_arm64.deb
postgresql-19-pg-command-fw0.1.0u26.x86_64pigsty655.9 KiBpostgresql-19-pg-command-fw_0.1.0-4PGSTY~resolute_amd64.deb
postgresql-19-pg-command-fw0.1.0u26.aarch64pigsty610.3 KiBpostgresql-19-pg-command-fw_0.1.0-4PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pg_command_fw_180.1.0el8.x86_64pigsty864.7 KiBpg_command_fw_18-0.1.0-3PIGSTY.el8.x86_64.rpm
pg_command_fw_180.1.0el8.aarch64pigsty775.1 KiBpg_command_fw_18-0.1.0-3PIGSTY.el8.aarch64.rpm
pg_command_fw_180.1.0el9.x86_64pigsty885.7 KiBpg_command_fw_18-0.1.0-4PGSTY.el9.x86_64.rpm
pg_command_fw_180.1.0el9.aarch64pigsty853.2 KiBpg_command_fw_18-0.1.0-4PGSTY.el9.aarch64.rpm
pg_command_fw_180.1.0el10.x86_64pigsty885.1 KiBpg_command_fw_18-0.1.0-4PGSTY.el10.x86_64.rpm
pg_command_fw_180.1.0el10.aarch64pigsty856.6 KiBpg_command_fw_18-0.1.0-4PGSTY.el10.aarch64.rpm
postgresql-18-pg-command-fw0.1.0d12.x86_64pigsty597.8 KiBpostgresql-18-pg-command-fw_0.1.0-4PGSTY~bookworm_amd64.deb
postgresql-18-pg-command-fw0.1.0d12.aarch64pigsty521.1 KiBpostgresql-18-pg-command-fw_0.1.0-4PGSTY~bookworm_arm64.deb
postgresql-18-pg-command-fw0.1.0d13.x86_64pigsty596.9 KiBpostgresql-18-pg-command-fw_0.1.0-4PGSTY~trixie_amd64.deb
postgresql-18-pg-command-fw0.1.0d13.aarch64pigsty521.6 KiBpostgresql-18-pg-command-fw_0.1.0-4PGSTY~trixie_arm64.deb
postgresql-18-pg-command-fw0.1.0u22.x86_64pigsty664.4 KiBpostgresql-18-pg-command-fw_0.1.0-4PGSTY~jammy_amd64.deb
postgresql-18-pg-command-fw0.1.0u22.aarch64pigsty617.2 KiBpostgresql-18-pg-command-fw_0.1.0-4PGSTY~jammy_arm64.deb
postgresql-18-pg-command-fw0.1.0u24.x86_64pigsty655.2 KiBpostgresql-18-pg-command-fw_0.1.0-4PGSTY~noble_amd64.deb
postgresql-18-pg-command-fw0.1.0u24.aarch64pigsty609.8 KiBpostgresql-18-pg-command-fw_0.1.0-4PGSTY~noble_arm64.deb
postgresql-18-pg-command-fw0.1.0u26.x86_64pigsty653.1 KiBpostgresql-18-pg-command-fw_0.1.0-4PGSTY~resolute_amd64.deb
postgresql-18-pg-command-fw0.1.0u26.aarch64pigsty607.8 KiBpostgresql-18-pg-command-fw_0.1.0-4PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pg_command_fw_170.1.0el8.x86_64pigsty861.5 KiBpg_command_fw_17-0.1.0-3PIGSTY.el8.x86_64.rpm
pg_command_fw_170.1.0el8.aarch64pigsty772.7 KiBpg_command_fw_17-0.1.0-3PIGSTY.el8.aarch64.rpm
pg_command_fw_170.1.0el9.x86_64pigsty880.1 KiBpg_command_fw_17-0.1.0-4PGSTY.el9.x86_64.rpm
pg_command_fw_170.1.0el9.aarch64pigsty848.6 KiBpg_command_fw_17-0.1.0-4PGSTY.el9.aarch64.rpm
pg_command_fw_170.1.0el10.x86_64pigsty881.3 KiBpg_command_fw_17-0.1.0-4PGSTY.el10.x86_64.rpm
pg_command_fw_170.1.0el10.aarch64pigsty851.3 KiBpg_command_fw_17-0.1.0-4PGSTY.el10.aarch64.rpm
postgresql-17-pg-command-fw0.1.0d12.x86_64pigsty594.5 KiBpostgresql-17-pg-command-fw_0.1.0-4PGSTY~bookworm_amd64.deb
postgresql-17-pg-command-fw0.1.0d12.aarch64pigsty520.3 KiBpostgresql-17-pg-command-fw_0.1.0-4PGSTY~bookworm_arm64.deb
postgresql-17-pg-command-fw0.1.0d13.x86_64pigsty594.3 KiBpostgresql-17-pg-command-fw_0.1.0-4PGSTY~trixie_amd64.deb
postgresql-17-pg-command-fw0.1.0d13.aarch64pigsty520.2 KiBpostgresql-17-pg-command-fw_0.1.0-4PGSTY~trixie_arm64.deb
postgresql-17-pg-command-fw0.1.0u22.x86_64pigsty661.6 KiBpostgresql-17-pg-command-fw_0.1.0-4PGSTY~jammy_amd64.deb
postgresql-17-pg-command-fw0.1.0u22.aarch64pigsty614.8 KiBpostgresql-17-pg-command-fw_0.1.0-4PGSTY~jammy_arm64.deb
postgresql-17-pg-command-fw0.1.0u24.x86_64pigsty653.3 KiBpostgresql-17-pg-command-fw_0.1.0-4PGSTY~noble_amd64.deb
postgresql-17-pg-command-fw0.1.0u24.aarch64pigsty606.8 KiBpostgresql-17-pg-command-fw_0.1.0-4PGSTY~noble_arm64.deb
postgresql-17-pg-command-fw0.1.0u26.x86_64pigsty650.4 KiBpostgresql-17-pg-command-fw_0.1.0-4PGSTY~resolute_amd64.deb
postgresql-17-pg-command-fw0.1.0u26.aarch64pigsty604.6 KiBpostgresql-17-pg-command-fw_0.1.0-4PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pg_command_fw_160.1.0el8.x86_64pigsty861.0 KiBpg_command_fw_16-0.1.0-3PIGSTY.el8.x86_64.rpm
pg_command_fw_160.1.0el8.aarch64pigsty770.9 KiBpg_command_fw_16-0.1.0-3PIGSTY.el8.aarch64.rpm
pg_command_fw_160.1.0el9.x86_64pigsty879.3 KiBpg_command_fw_16-0.1.0-4PGSTY.el9.x86_64.rpm
pg_command_fw_160.1.0el9.aarch64pigsty846.5 KiBpg_command_fw_16-0.1.0-4PGSTY.el9.aarch64.rpm
pg_command_fw_160.1.0el10.x86_64pigsty879.3 KiBpg_command_fw_16-0.1.0-4PGSTY.el10.x86_64.rpm
pg_command_fw_160.1.0el10.aarch64pigsty847.5 KiBpg_command_fw_16-0.1.0-4PGSTY.el10.aarch64.rpm
postgresql-16-pg-command-fw0.1.0d12.x86_64pigsty593.6 KiBpostgresql-16-pg-command-fw_0.1.0-4PGSTY~bookworm_amd64.deb
postgresql-16-pg-command-fw0.1.0d12.aarch64pigsty519.6 KiBpostgresql-16-pg-command-fw_0.1.0-4PGSTY~bookworm_arm64.deb
postgresql-16-pg-command-fw0.1.0d13.x86_64pigsty593.3 KiBpostgresql-16-pg-command-fw_0.1.0-4PGSTY~trixie_amd64.deb
postgresql-16-pg-command-fw0.1.0d13.aarch64pigsty519.1 KiBpostgresql-16-pg-command-fw_0.1.0-4PGSTY~trixie_arm64.deb
postgresql-16-pg-command-fw0.1.0u22.x86_64pigsty661.7 KiBpostgresql-16-pg-command-fw_0.1.0-4PGSTY~jammy_amd64.deb
postgresql-16-pg-command-fw0.1.0u22.aarch64pigsty613.5 KiBpostgresql-16-pg-command-fw_0.1.0-4PGSTY~jammy_arm64.deb
postgresql-16-pg-command-fw0.1.0u24.x86_64pigsty655.0 KiBpostgresql-16-pg-command-fw_0.1.0-4PGSTY~noble_amd64.deb
postgresql-16-pg-command-fw0.1.0u24.aarch64pigsty605.6 KiBpostgresql-16-pg-command-fw_0.1.0-4PGSTY~noble_arm64.deb
postgresql-16-pg-command-fw0.1.0u26.x86_64pigsty649.1 KiBpostgresql-16-pg-command-fw_0.1.0-4PGSTY~resolute_amd64.deb
postgresql-16-pg-command-fw0.1.0u26.aarch64pigsty603.7 KiBpostgresql-16-pg-command-fw_0.1.0-4PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pg_command_fw_150.1.0el8.x86_64pigsty849.0 KiBpg_command_fw_15-0.1.0-3PIGSTY.el8.x86_64.rpm
pg_command_fw_150.1.0el8.aarch64pigsty760.1 KiBpg_command_fw_15-0.1.0-3PIGSTY.el8.aarch64.rpm
pg_command_fw_150.1.0el9.x86_64pigsty871.9 KiBpg_command_fw_15-0.1.0-4PGSTY.el9.x86_64.rpm
pg_command_fw_150.1.0el9.aarch64pigsty840.4 KiBpg_command_fw_15-0.1.0-4PGSTY.el9.aarch64.rpm
pg_command_fw_150.1.0el10.x86_64pigsty871.6 KiBpg_command_fw_15-0.1.0-4PGSTY.el10.x86_64.rpm
pg_command_fw_150.1.0el10.aarch64pigsty840.6 KiBpg_command_fw_15-0.1.0-4PGSTY.el10.aarch64.rpm
postgresql-15-pg-command-fw0.1.0d12.x86_64pigsty590.4 KiBpostgresql-15-pg-command-fw_0.1.0-4PGSTY~bookworm_amd64.deb
postgresql-15-pg-command-fw0.1.0d12.aarch64pigsty516.7 KiBpostgresql-15-pg-command-fw_0.1.0-4PGSTY~bookworm_arm64.deb
postgresql-15-pg-command-fw0.1.0d13.x86_64pigsty590.1 KiBpostgresql-15-pg-command-fw_0.1.0-4PGSTY~trixie_amd64.deb
postgresql-15-pg-command-fw0.1.0d13.aarch64pigsty516.3 KiBpostgresql-15-pg-command-fw_0.1.0-4PGSTY~trixie_arm64.deb
postgresql-15-pg-command-fw0.1.0u22.x86_64pigsty657.5 KiBpostgresql-15-pg-command-fw_0.1.0-4PGSTY~jammy_amd64.deb
postgresql-15-pg-command-fw0.1.0u22.aarch64pigsty610.8 KiBpostgresql-15-pg-command-fw_0.1.0-4PGSTY~jammy_arm64.deb
postgresql-15-pg-command-fw0.1.0u24.x86_64pigsty647.6 KiBpostgresql-15-pg-command-fw_0.1.0-4PGSTY~noble_amd64.deb
postgresql-15-pg-command-fw0.1.0u24.aarch64pigsty602.0 KiBpostgresql-15-pg-command-fw_0.1.0-4PGSTY~noble_arm64.deb
postgresql-15-pg-command-fw0.1.0u26.x86_64pigsty645.2 KiBpostgresql-15-pg-command-fw_0.1.0-4PGSTY~resolute_amd64.deb
postgresql-15-pg-command-fw0.1.0u26.aarch64pigsty600.2 KiBpostgresql-15-pg-command-fw_0.1.0-4PGSTY~resolute_arm64.deb

源码

pig build pkg pg_command_fw;		# build rpm/deb

安装

启用 PGDG 和 PIGSTY 仓库并更新缓存。

pig repo add pgsql -u   # add both repo and update cache

使用 pig 安装扩展。

pig install pg_command_fw;		# install by package name

pig install pg_command_fw -v 19;   # install for PG 19
pig install pg_command_fw -v 18;   # install for PG 18
pig install pg_command_fw -v 17;   # install for PG 17
pig install pg_command_fw -v 16;   # install for PG 16
pig install pg_command_fw -v 15;   # install for PG 15

将扩展加入 shared_preload_libraries,详见配置说明。

shared_preload_libraries = 'pg_command_fw';

在数据库中创建扩展。

CREATE EXTENSION pg_command_fw;

用法

pg_command_fw 是 PostgreSQL 命令防火墙。它通过 ProcessUtility 钩子拦截 DDL 和工具命令,并通过解析后分析钩子阻断部分内置文件读取函数。每个命令类别都有自己的 GUC 控制。

启用扩展

该扩展必须预加载:

shared_preload_libraries = 'pg_command_fw'

然后在数据库中启用:

CREATE EXTENSION pg_command_fw;

Pigsty 包元数据记录版本 0.1.0,覆盖 PostgreSQL 15-18,并说明需要预加载才能在所有会话中激活钩子。上游 README 也记录 PostgreSQL 15-18 支持。

命令类别

上游 README 记录了这些防火墙类别:

  • TRUNCATE:pg_command_fw.block_truncate,默认 on,阻断非超级用户。
  • DROP TABLE:pg_command_fw.block_drop_table,默认 off,启用后阻断非超级用户。
  • ALTER SYSTEM:pg_command_fw.block_alter_system,默认 on,阻断所有人。
  • LOAD:pg_command_fw.block_load,默认 on,阻断所有人。
  • COPY ... PROGRAM:pg_command_fw.block_copy_program,默认 on,阻断所有人。
  • 普通 COPY:pg_command_fw.block_copy,默认 off,启用后阻断非超级用户。
  • pg_read_file()、pg_read_binary_file() 和 pg_stat_file():pg_command_fw.block_read_file,默认 on,阻断所有人。

部分类别只阻断非超级用户,另一些类别阻断包括超级用户在内的所有人。除非显式列入 pg_command_fw.blocked_roles,超级用户只会豁免非超级用户类别。

重要 GUC

  • pg_command_fw.enabled:启用或禁用所有检查
  • pg_command_fw.block_truncate
  • pg_command_fw.block_drop_table
  • pg_command_fw.production_schemas
  • pg_command_fw.block_alter_system
  • pg_command_fw.block_load
  • pg_command_fw.block_copy_program
  • pg_command_fw.block_copy
  • pg_command_fw.block_read_file
  • pg_command_fw.blocked_roles
  • pg_command_fw.hint
  • pg_command_fw.audit_log_enabled

设置 production_schemas 时,DROP TABLE 检查仅限这些模式中显式带模式名的表名;README 说明未限定名称不会通过 search_path 解析。

审计日志

扩展会在 command_fw.audit_log 中记录被拦截的命令。README 记录的列包括:

  • timestamp 时间戳
  • session 和 current user 名称
  • 原始查询文本
  • 命令类型
  • 目标模式或对象
  • 客户端地址
  • 命令是否被阻断
  • 内部阻断原因

被阻断的审计插入只会尽力完成,因为该行会随被阻断事务回滚;请使用 PostgreSQL 服务器日志作为被阻断事件的权威记录。

示例

在生产模式中阻断 TRUNCATE 和 DROP TABLE:

ALTER SYSTEM SET pg_command_fw.block_truncate = on;
ALTER SYSTEM SET pg_command_fw.block_drop_table = on;
ALTER SYSTEM SET pg_command_fw.production_schemas = 'public,payments';
ALTER SYSTEM SET pg_command_fw.hint = 'Contact your DBA to request access';
SELECT pg_reload_conf();

阻断某个角色执行任何受管命令:

ALTER SYSTEM SET pg_command_fw.blocked_roles = 'app_deploy';
SELECT pg_reload_conf();

在维护会话中临时禁用防火墙:

SET pg_command_fw.enabled = off;
TRUNCATE big_table;
SET pg_command_fw.enabled = on;

表格阅读器