cat
、tail -f
、less
、grep
等命令查看日志文件,如tail -f /var/log/postgresql/postgresql-{版本号}-main.log
可实时监控日志。postgresql.conf
,关键参数包括:
logging_collector
:启用日志收集(建议设为on
)。log_directory
:指定日志存储目录(如pg_log
)。log_filename
:定义命名规则(如含时间戳postgresql-%Y-%m-%d_%H%M%S.log
)。log_statement
:控制记录SQL语句类型(如ddl
记录结构变更)。log_min_duration_statement
:记录慢查询阈值(单位:毫秒)。logrotate
配置轮转规则,例如每天轮转并保留7天日志:# /etc/logrotate.d/postgresql 配置示例
/var/log/postgresql/*.log {
daily
rotate 7
compress
missingok
create 0640 postgres adm
}
log_rotation_age
(按时间轮转,如1d
)和log_rotation_size
(按大小轮转,如100MB
)设置。pgBadger
:生成HTML报告,分析查询性能、错误等。log_line_prefix
和参数控制不记录或脱敏。