postgreSQL9.3日志配置

pg的日志配置选项很多,默认的情况下pg只是记录了启动停止的信息。在postgresql.conf配置文件中,你可以配置日志记录在哪里,日志记录什么,日志什么时候记录。如下面的是我自己测试的设置

# - When to Log -


client_min_messages = notice # values in order of decreasing detail:
#   debug5
#   debug4
#   debug3
#   debug2
#   debug1
#   log
#   notice
#   warning
#   error


log_min_messages = warning # values in order of decreasing detail:
#   debug5
#   debug4
#   debug3
#   debug2
#   debug1
#   info
#   notice
#   warning
#   error
#   log
#   fatal
#   panic


log_min_error_statement = error # values in order of decreasing detail:
#   debug5
#   debug4
#   debug3
#   debug2
#   debug1
#   info
#   notice
#   warning
#   error
#   log
#   fatal
#   panic (effectively off)


log_min_duration_statement = 0 # -1 is disabled, 0 logs all statements
# and their durations, > 0 logs only
# statements running at least this number
# of milliseconds




# - What to Log -


#debug_print_parse = off
#debug_print_rewritten = off
debug_print_plan = on
#debug_pretty_print = on
#log_checkpoints = off
log_connections = on
log_disconnections = on
#log_duration = off
#log_error_verbosity = default # terse, default, or verbose messages
#log_hostname = off
log_line_prefix = '%d%p%c%t ' # special values:
#   %a = application name
#   %u = user name
#   %d = database name
#   %r = remote host and port
#   %h = remote host
#   %p = process ID
#   %t = timestamp without milliseconds
#   %m = timestamp with milliseconds
#   %i = command tag
#   %e = SQL state
#   %c = session ID
#   %l = session line number
#   %s = session start timestamp
#   %v = virtual transaction ID
#   %x = transaction ID (0 if none)
#   %q = stop here in non-session
#        processes
#   %% = '%'
# e.g. '<%u%%%d> '
#log_lock_waits = off # log lock waits >= deadlock_timeout
#log_statement = 'none' # none, ddl, mod, all
#log_temp_files = -1 # log temporary files equal or larger
# than the specified size in kilobytes;
# -1 disables, 0 logs all temp files
log_timezone = 'Asia/Hong_Kong'

在log_line_prefix中记录了数据库名字,进程id,会话id,时间。

log_connection记录了连接的信息等等,这些都是可以按自己的需要来指定的。

下面测试将日志放到csv文件中

log_destination = 'csvlog' # Valid values are combinations of
# stderr, csvlog, syslog, and eventlog,
# depending on platform.  csvlog
# requires logging_collector to be on.


# This is used when logging to stderr:
logging_collector = on # Enable capturing of stderr and csvlog
# into log files. Required to be on for
# csvlogs.
# (change requires restart)


# These are only used if logging_collector is on:
log_directory = 'D:\\PostgreSQL\\9.3\\data\\logs' # directory where log files are written,
# can be absolute or relative to PGDATA
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' # log file name pattern,

设置这几个参数后,重启db就可以生成csv格式的日志了,这种日志可以方便的将日志文件放入到数据库的表中。下面创建日志表

CREATE TABLE postgres_log
(
  log_time timestamp(3) with time zone,
  user_name text,
  database_name text,
  process_id integer,
  connection_from text,
  session_id text,
  session_line_num bigint,
  command_tag text,
  session_start_time timestamp with time zone,
  virtual_transaction_id text,
  transaction_id bigint,
  error_severity text,
  sql_state_code text,
  message text,
  detail text,
  hint text,
  internal_query text,
  internal_query_pos integer,
  context text,
  query text,
  query_pos integer,
  location text,
  application_name text,
  PRIMARY KEY (session_id,session_line_num)
);
使用copy将日志文件拷贝到表中
copy postgres_log from 'D:\\PostgreSQL\\9.3\\data\\logs\\postgresql-2014-11-17_210250.csv' with csv;

拷贝表到文件中:copy  customer to  'D:\\test2.txt';

这个copy在表和文件中移动数据很方便。


日志配置推荐设置的参数
log_line_prefix='%t:%r:%u@%d:[%p]:' 记录日志的内容
log_statement记录日志的类型
log_min_duration_statement记录超过多长时间的sql

你可能感兴趣的:(PostgreSQL管理)