加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

通过file_fdw读取PostgreSQL日志文件(PG9.1新增)

发布时间:2020-12-13 17:22:48 所属栏目:百科 来源:网络整理
导读:PG9.4官方英文文档: 关于file_fdw: http://www.postgresql.org/docs/9.4/static/file-fdw.html 关于日志配置 :http://www.postgresql.org/docs/9.4/static/runtime-config-logging.html 1、 配置 postgresql.conf 文件中关于日志的参数 log_destination='

PG9.4官方英文文档:

关于file_fdw:http://www.postgresql.org/docs/9.4/static/file-fdw.html

关于日志配置:http://www.postgresql.org/docs/9.4/static/runtime-config-logging.html


1、 配置postgresql.conf文件中关于日志的参数

log_destination='csvlog'#生成日csv格式的日志文件
logging_collector=on
log_directory='pg_log'#日志文件存储在./data/pg_log目录
log_filename='postgresql-%Y-%m-%d'#定义日志文件名字为postgresql-2013-12-17.csv
log_truncate_on_rotation=off
log_rotation_age=1d#设置日志文件生成的频率为1天
log_rotation_size=0
log_error_verbosity=verbose#设置日志文件中的错误信息为详细
log_statement=all#设置所有语句均计入日志

2、配置file_fdw并创建外部表

检查pg安装目录的lib/postgresql目录下是否存在file_fdw.so文件,确保安装时已经编译安装了file_fdw组件。

以下需要以数据库postgres用户连接到postgres数据库进行操作:

创建扩展:

postgres=#createextensionfile_fdw;

创建外部服务:

postgres=#createserverpglogFOREIGNDATAWRAPPERfile_fdw;

创建外部表:

postgres=#CREATEFOREIGNTABLEpglog(
log_timetimestamp(3)withtimezone,user_nametext,database_nametext,process_idinteger,connection_fromtext,session_idtext,session_line_numbigint,command_tagtext,session_start_timetimestampwithtimezone,virtual_transaction_idtext,transaction_idbigint,error_severitytext,sql_state_codetext,messagetext,detailtext,hinttext,internal_querytext,internal_query_posinteger,contexttext,querytext,query_posinteger,locationtext,application_nametext
)SERVERpglog
OPTIONS(filename'/user/data/pg_log/postgresql-2013-12-17.csv',format'csv');

注意:以上filename中的文件路径要求是绝对路径。

可以用以下SQL删除外部表:

postgres=#dropforeigntablepglog;

3、使用外部表读取日志信息

postgres=#selectlog_time,connection_from,user_name,database_name,query,application_namefrompglogwherequeryisnotnull;

实际应用中可以根据需要为SQL语句添加过滤条件

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读