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."

PostgreSQL per database or per user audit use pg_log_userqueries相关推荐

  1. 02=windows下安装PostgreSQL(The database cluster initialisation failed)

    The database cluster initialisation failed. windows下安装报错:The database cluster initialisation failed. ...

  2. Oracle Database 12c Security - 12. Audit for Accountability

    Oracle Unified Audit Trail (OUA)是12c新增功能. THE SECURITY CYCLE 审计使安全更完整,审计是事后行为,不能预防. 访问控制并不能保证非授权访问,人 ...

  3. Postgresql ERROR: database is not accepting commands to avoid wraparound data loss in database

    ERROR:  database is not accepting commands to avoid wraparound data loss in database "mydb" ...

  4. PostgreSQL 数据库安全指南

    PostgreSQL 数据库安全指南 TAG 27 , TAG 6 作者 digoal 日期 2015-06-01 标签 PostgreSQL , 数据库安全指南 背景 数据库在一个企业中通常都处于非 ...

  5. zabbix nginx php postgresql,debian10安装zabbix4.2+nginx+postgresql

    最近在做zabbix监控,网上很多都是用centos+mariadb(mysql)搭建的,不适合我喜爱的debian系统,特意去bing搜索了一遍国外的教程,特此笔录 1.挑选部分依赖安装 sudo ...

  6. UbuntuでPostgreSQLをインストールからリモートアクセスまでの手順

    PostgreSQLサーバの立ち上げに少しハマりましたので.メモしておきます. OS: Ubuntu14.04 LTS インストール 最初はPostgreSQLをインストールします. 普通にapt-g ...

  7. PostgreSQL 恢复模式错误日志增强 - 提供正在恢复的WAL(XLOG)文件位置

    标签 PostgreSQL , the database system is starting up , the database system is in recovery mode 背景 当数据库 ...

  8. postgresql网站翻译

    http://www.postgresql.org/download/ Search 搜索 Donate 捐赠 Contact 联系 PostgreSQL The world's most advan ...

  9. 【postgresql初始化失败】initdb: invalid locale settings; check LANG and LC_* environment variables

    The files belonging to this database system will be owned by user "postgres". This user mu ...

最新文章

  1. 传递结构体变量解决方案,资料整理一
  2. 八十五、再探希尔排序,桶排序,计数排序和基数排序
  3. 查WiFi密码的三种方法
  4. lintcode-111-爬楼梯
  5. 《Hadoop权威指南》知识点整理5
  6. 认知无线电----能量检测法原理介绍及MATLAB实现
  7. 整人小程序【转自CSDN】
  8. 新浪微博开放平台API访问频率限制解决方法
  9. 直播预告|年营业额百亿的企业都在如何做数字化转型
  10. Java计算圆的周长代码,Java基础之计算圆的周长
  11. 《安富莱嵌入式周报》第275期:2022.07.18--2022.07.24
  12. 实用的 PyYAML 使用技巧
  13. CA410_SDK测试及读数软件
  14. java国际象棋棋牌_用java画国际象棋棋盘
  15. C++字符串的压缩和解压(初学者的)
  16. 猫扑网互动营销六大模式
  17. 修改QtCreater界面大小
  18. Dynamics CRM 解决方案操作
  19. html格式打开软件打开方式,eps用什么软件打开方式(一文教你eps文件打开流程)...
  20. 用程序判断1993年3月1日是星期几

热门文章

  1. python 全栈开发,Day54(关于DOM操作的相关案例,JS中的面向对象,定时器,BOM,client、offset、scroll系列)...
  2. COMPUTEX展 CPX论坛聚焦AI与物联网
  3. Criteo公司在上海新开通一个数据中心
  4. 微软Skype推出Slack整合预览版
  5. java多线程基本概述(二)——Thread的一些方法
  6. git暂存工作区stash
  7. win7 启用网络发现
  8. IBM加入OpenOffice 贡献Lotus Notes程序代码
  9. Olympic Games -basketball
  10. bootstrap今天的学习心得