set_user

增加了日志记录的 SET ROLE

概览

扩展包名版本分类许可证语言
set_user4.2.0SECPostgreSQLC
ID扩展名BinLibLoadCreateTrustReloc模式
7370set_user-
相关扩展pg_readonly pg_permissions pgaudit login_hook pgauditlogtofile pg_auth_mon credcheck pgextwlist

版本

类型仓库版本PG 大版本包名依赖
EXTPGDG4.2.01817161514set_user-
RPMPGDG4.2.01817161514set_user_$v-
DEBPGDG4.2.01817161514postgresql-$v-set-user-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
PGDG 4.2.0
PGDG 4.2.0
PGDG 4.2.0
PGDG 4.2.0
PGDG 4.2.0
d12.aarch64
PGDG 4.2.0
PGDG 4.2.0
PGDG 4.2.0
PGDG 4.2.0
PGDG 4.2.0
d13.x86_64
PGDG 4.2.0
PGDG 4.2.0
PGDG 4.2.0
PGDG 4.2.0
PGDG 4.2.0
d13.aarch64
PGDG 4.2.0
PGDG 4.2.0
PGDG 4.2.0
PGDG 4.2.0
PGDG 4.2.0
u22.x86_64
u22.aarch64
PGDG 4.2.0
PGDG 4.2.0
PGDG 4.2.0
PGDG 4.2.0
PGDG 4.2.0
u24.x86_64
u24.aarch64
PGDG 4.2.0
PGDG 4.2.0
PGDG 4.2.0
PGDG 4.2.0
PGDG 4.2.0

安装

您可以直接安装 set_user 扩展包的预置二进制包,首先确保 PGDG 仓库已经添加并启用:

pig repo add pgdg -u          # 添加 PGDG 仓库并更新缓存

使用 pig 或者是 apt/yum/dnf 安装扩展:

pig install set_user;          # 当前活跃 PG 版本安装
pig ext install -y set_user -v 18  # PG 18
pig ext install -y set_user -v 17  # PG 17
pig ext install -y set_user -v 16  # PG 16
pig ext install -y set_user -v 15  # PG 15
pig ext install -y set_user -v 14  # PG 14
dnf install -y set_user_18       # PG 18
dnf install -y set_user_17       # PG 17
dnf install -y set_user_16       # PG 16
dnf install -y set_user_15       # PG 15
dnf install -y set_user_14       # PG 14
apt install -y postgresql-18-set-user   # PG 18
apt install -y postgresql-17-set-user   # PG 17
apt install -y postgresql-16-set-user   # PG 16
apt install -y postgresql-15-set-user   # PG 15
apt install -y postgresql-14-set-user   # PG 14

预加载配置

shared_preload_libraries = 'set_user';

创建扩展

CREATE EXTENSION set_user;

用法

set_user: 带增强日志和控制的用户切换

set_user 允许切换用户和可选的权限提升,并提供增强的审计日志。当非特权用户必须提升为超级用户或对象所有者角色进行维护任务时,它提供额外的控制层。

CREATE EXTENSION set_user;

配置

添加到 postgresql.conf

shared_preload_libraries = 'set_user'
参数默认值描述
set_user.block_alter_systemon提升权限后阻止 ALTER SYSTEM
set_user.block_copy_programon提升权限后阻止 COPY PROGRAM
set_user.block_log_statementon阻止 SET log_statement;对超级用户强制 log_statement=all
set_user.superuser_allowlist*允许提升为超级用户的角色
set_user.nosuperuser_target_allowlist*允许作为非超级用户目标的角色
set_user.superuser_audit_tagAUDIT提升权限时附加到 log_line_prefix 的标签

函数

-- 切换到非超级用户角色
SELECT set_user('dbclient');

-- 提升为超级用户(需要 set_user_u 的 EXECUTE 权限)
SELECT set_user_u('postgres');

-- 使用令牌切换(重置时需要令牌)
SELECT set_user('dbclient', 'my_secret_token');

-- 重置回原始用户
SELECT reset_user();
SELECT reset_user('my_secret_token');  -- 如果使用了令牌

-- 不可撤销的会话认证切换
SELECT set_session_auth('target_role');

权限设置

-- 允许角色切换到非超级用户角色
GRANT EXECUTE ON FUNCTION set_user(text) TO admin;

-- 允许角色提升为超级用户
GRANT EXECUTE ON FUNCTION set_user_u(text) TO dba;

提升权限时的行为

当提升为超级用户角色时:

  • 角色转换会以特定标记记录
  • ALTER SYSTEMCOPY PROGRAM 被阻止(如已配置)
  • log_statement 被强制设为 all 以实现完整审计追踪
  • AUDIT 标签被附加到 log_line_prefix

最后修改 2026-03-14: update extension metadata (953cbd0)