PostgreSQL per database or per user audit use pg_log_userqueries

PostgreSQL 本身并不带针对某数据库或某用户的SQL审计功能。
通过pg_log_userqueries可以达到这样的目的。

首先到pgxn下载pg_log_userqueries模块 : 

我测试的是0.4.0版本。

1. 安装

# unzip pg_log_userqueries-0.4.0.zip
# mv pg_log_userqueries-0.4.0 /opt/soft_bak/postgresql-9.1.2/contrib/
# . /home/postgres/.bash_profile
> cd /opt/soft_bak/postgresql-9.1.2/contrib/pg_log_userqueries-0.4.0
> make && make install

2. 修改postgresql.conf 配置文件 : 

shared_preload_libraries = 'pg_log_userqueries'          # 如果以前已经配置了其他模块,则用逗号隔开.
custom_variable_classes = 'pg_log_userqueries'          # list of custom variable class names
pg_log_userqueries.log_db = 'digoal'
pg_log_userqueries.log_user = 'digoal'
pg_log_userqueries.syslog_facility = 'LOCAL0'
pg_log_userqueries.syslog_ident = 'pg_log_userqueries'
pg_log_userqueries.log_level = 'NOTICE'
pg_log_userqueries.log_label = 'user query: '
pg_log_userqueries.log_destination = 'syslog'

2.1 解释 : 

log_db='digoal', 表示我需要审计digoal库的所有查询,
log_user='digoal' 表示我需要审计digoal用户的所有查询
log_level = 'NOTICE' 审计的日志级别.

3. 我这里配置的log_destination是syslog, 因此需要配置操作系统的syslog.conf。

可以参考我前一篇BLOG。例如  : 

3.1. 配置操作系统/etc/syslog.conf

把local0.*;加入到以下行的头部 :
*.info;mail.none;authpriv.none;cron.none               /var/log/messages
更改后变成
local0.*;*.info;mail.none;authpriv.none;cron.none               /var/log/messages

3.2. 重启syslog服务

service syslog restart

3.3. 确保syslog服务是自动启动的,

chkconfig --list|grep syslog
syslog          0:off   1:off   2:on    3:on    4:on    5:on    6:off

4. 重启数据库

pg_ctl stop -m fast -D $PGDATA
pg_ctl start -D $PGDATA
Jan 19 11:15:26 db-172 postgres[6229]: [1-1] LOG:  loaded library "pg_log_userqueries"

5. 测试 : 

5.1 查看连接到digoal库的审计

postgres@db-172-> psql -h 127.0.0.1 -U postgres digoal
psql (9.1.2)
Type "help" for help.
digoal=# select now();
              now
-------------------------------
 2012-01-19 11:57:30.678585+08

日志 : 

Jan 19 11:57:30 db-172 postgres[6690]: [1] user query: select now();

5.2 查看用户digoal的审计

postgres@db-172-> psql -h 127.0.0.1 -U digoal postgres
psql (9.1.2)
Type "help" for help.
postgres=> select current_date;
    date
------------
 2012-01-19

日志 : 

Jan 19 12:00:22 db-172 postgres[6729]: [1]  user query : select current_date;

5.3 验证非审计范围的SQL无输出

postgres=> \c postgres postgres
You are now connected to database "postgres" as user "postgres".
postgres=# select 1;
 ?column?
----------
        1
(1 row)

日志 : 

【缺陷】

暂时没有办法记录用户名和库名. 例如配置以下无法解析到用户名和库名 : 

希望下一版本可以支持.

pg_log_userqueries.log_label = '"$dbname" "$user": '

【参考】

http://pgxn.org/dist/pg_log_userqueries/0.4.0/

可配置参数 ( 截取自源代码文件) :

DefineCustomStringVariable( "pg_log_userqueries.log_label",
  "Label in front of the user query."
DefineCustomStringVariable( "pg_log_userqueries.log_user",
  "Log statement according to the given user."
DefineCustomStringVariable( "pg_log_userqueries.log_db",
  "Log statement according to the given database."
DefineCustomEnumVariable( "pg_log_userqueries.log_destination",
  "Selects log destination (either stderr or syslog)."
DefineCustomEnumVariable( "pg_log_userqueries.syslog_facility",
  "Selects syslog level of log (same options than PostgreSQL syslog_facility)."
DefineCustomStringVariable( "pg_log_userqueries.syslog_ident",
  "Select syslog program identity name."
DefineCustomEnumVariable( "pg_log_userqueries.log_level",
  "Selects level of log (same options than log_min_messages."
时间: 2024-09-08 09:51:26

PostgreSQL per database or per user audit use pg_log_userqueries的相关文章

PostgreSQL schema,database owner 的高危注意事项

云用户反映的一个问题,引发一系列安全思考.以下是创建PostgreSQL schema的语法说明页的一个note:http://www.postgresql.org/docs/9.5/static/sql-createschema.html According to the SQL standard, the owner of a schema always owns all objects within it. PostgreSQL allows schemas to contain obje

PostgreSQL Fine-Grained Table,Column,Row Level Audit

通过配置用户级或数据库级的参数可以实现用户以及数据库级别的审计, 但是这样的粒度可能还是太粗糙了. 如果需要更细致的审计, 例如针对某些表的操作审计, 某些用户对某些表的审计, 或者仅仅当某个列的值发生变化时才被审计(记录到LOG或表里面, 本文的例子是将审计信息输出到LOG, 使用raise). 这样的需求可以通过触发器来实现. 接下来以PostgreSQL 9.2为例进行讲解. # 基础的参数配置 log_destination = 'csvlog' logging_collector =

又来勒索,有完没完 - 数据库安全指南

背景 数据库在一个企业中通常都处于非常核心的位置,数据库安全是一个非常严肃的话题. 从机房.网络.服务器.数据交换设备.操作系统.应用程序.数据库本身,数据库所处的环境非常复杂,安全隐患也非常多. 所以本文将从各个层面帮助大家理解和避免一些常见的安全隐患问题. 本文是PostgreSQL使用安全指导性的文章,涉及详细的用法或原理请参考相关链接. 如何安全的使用PostgreSQL,让用户高枕无忧呢? 可以分为如下几个方面来加固你的数据库. 一.认证安全 认证前的安全,端口暴露度的把握. Post

[example]Sync data from PostgreSQL database to another PostgreSQL database

利用EDB的database link实现从PostgreSQL到PostgreSQL的数据同步.源数据是持续插入,无更新操作的记录.将create_time作为同步标记.并且需要定期清除同步来的历史数据. Source Database : PostgreSQLDestination Database : PostgreSQLSync Database : EnterpriseDB SD_table :                                       Table "

PostgreSQL as RDS on AWS

亚马逊提供的RDS中包含oracle, sql server, mysql, postgresql. 是一个DaaS服务. 作为一个DaaS服务, AWS的RDS提供了哪些功能呢? 1. 数据库参数是预配置好的, 所以用户不需要对数据库进行配置. 2. 监控 3. 自动打补丁 4. 自动备份 5. 数据库快照(自动/手动接口) 6. 事件触发告警, 提供SMS短信, 邮件等接口 7. 存储provision(即可以预知IOPS和容量, 但是可能不使用这么多容量或IOPS, 有点类似宽带里面的共享

PostgreSQL · 特性分析 · 逻辑结构和权限体系

本文旨在帮助用户理解PostgreSQL的逻辑结构和权限体系,帮助用户快速的理解和管理数据库的权限. 逻辑结构 最上层是实例,实例中允许创建多个数据库,每个数据库中可以创建多个schema,每个schema下面可以创建多个对象. 对象包括表.物化视图.操作符.索引.视图.序列.函数.- 等等. 在数据库中所有的权限都和角色(用户)挂钩,public是一个特殊角色,代表所有人. 超级用户是有允许任意操作对象的,普通用户只能操作自己创建的对象. 另外有一些对象是有赋予给public角色默认权限的,所

PostgreSQL PHP 开发者手册

标签 PostgreSQL , Java , php , ruby , python , C 背景 转自 http://zetcode.com/db/postgresqlphp/ 正文 This is a PHP tutorial for the PostgreSQL database. It covers the basics of PostgreSQL programming with PHP. The examples were created and tested on Linux. T

PostgreSQL 多租户

PostgreSQL 多租户 作者 digoal 日期 2016-11-07 标签 PostgreSQL , 多租户 , schema , DATABASE , PDB , Oracle 12c 背景 Oracle 12c提出了数据库多租户的概念,即PDBs(私有数据库),因为早期Oracle的设计是以schema为隔离的,schema的隔离不够彻底,原因是通过赋权就很容易获得不同SCHEMA下的数据. PDB的设计与PostgreSQL的Database概念非常相似,所以PostgreSQL实

PostgreSQL Python 开发者手册

标签 PostgreSQL , Java , php , ruby , python , C 背景 转自 http://zetcode.com/db/postgresqlpythontutorial/ 正文 This is a Python programming tutorial for the PostgreSQL database. It covers the basics of PostgreSQL programming with the Python language. You mi