Azure Postgres单实例pgaudit:如何在DDL审计日志中显示操作用户?
问题描述
在Azure PostgreSQL单实例中已启用pgaudit,配置审计项为DDL和ROLE,并将存储账户设为日志存储目标。执行了以下测试操作:
postgres=> create table test_audit ( c1 integer ) ; CREATE TABLE postgres=> \dt test_audit List of relations Schema | Name | Type | Owner --------+------------+-------+------------ public | test_audit | table | posadmn001 (1 row) postgres=> insert into test_audit values ( 1 ) ; INSERT 0 1 postgres=> insert into test_audit values ( 2 ) ; INSERT 0 1 postgres=> alter table test_audit add c2 integer ; ALTER TABLE postgres=> drop table test_audit ; DROP TABLE
目前DDL操作的审计日志已正常生成,但日志消息中未包含执行操作的用户信息,例如:
Create Table
"message": "*AUDIT: SESSION,1,1,DDL,CREATE TABLE,TABLE,public.test_audit,create table test_audit ( c1 integer ) ;,<none>","detail":"","errorLevel":"LOG","domain":"postgres-11","schemaName":"","tableName":"","columnName":"","datatypeName"
希望在包含AUDIT关键字的日志消息中直接获取操作命令及执行用户,需调整哪些配置?
解决方法
要让pgaudit审计日志包含执行操作的用户信息,需调整Azure PostgreSQL单实例的核心参数:
修改
log_line_prefix参数
登录Azure门户,进入目标PostgreSQL实例的服务器参数页面,找到log_line_prefix,将其值设置为包含用户名的格式,示例:%t [%p] %u@%d参数说明:
%t:日志生成时间戳%p:进程ID%u:执行操作的用户名%d:当前操作的数据库名
设置后,审计日志的message字段会带上用户名信息,比如:
"message": "2024-05-20 10:00:00 [1234] posadmn001@postgres *AUDIT: SESSION,1,1,DDL,CREATE TABLE,TABLE,public.test_audit,create table test_audit ( c1 integer ) ;,posadmn001"确认
pgaudit.log参数配置
确保pgaudit.log已设置为ddl, role(当前配置已符合),该参数控制审计覆盖的操作类型,配合log_line_prefix的设置,用户信息会被纳入审计日志条目。重启实例生效
log_line_prefix属于需要重启才能生效的参数,在Azure门户的实例概述页点击重启,完成参数应用。
另外,确认pgaudit.log_level保持为LOG(当前已配置),该参数确保审计日志以标准级别输出,完整包含所有审计字段(包括用户信息)。
内容的提问来源于stack exchange,提问作者Roberto Hernandez
相关产品推荐
相关产品推荐

