/etc/postgresql/{version}/main/postgresql.conf,设置:logging_collector = onlog_directory = 'pg_log'(可自定义路径)log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'。log_min_messages:控制记录的日志级别(如debug5、info、error)。log_statement:记录特定SQL语句(如all、mod、none)。log_duration:记录SQL语句执行时长。sudo tail -f /var/log/postgresql/postgresql-{版本号}-main.log。sudo less /var/log/postgresql/postgresql-{版本号}-main.log。sudo grep 'ERROR' /var/log/postgresql/postgresql-{版本号}-main.log。/etc/logrotate.d/postgresql,示例配置:/var/log/postgresql/postgresql-{版本号}-main.log {
daily
rotate 7
compress
missingok
notifempty
}
```。
sudo pg_rotate_logfile(需先在postgresql.conf中启用log_rotation_age和log_rotation_size)。logrotate配置archive参数,将旧日志压缩存储到指定目录。rsync、scp或日志管理工具(如Logstash、Fluentd)定期传输日志到远程服务器。/var/log/postgresql/目录权限正确(通常为postgres:postgres)。sudo systemctl restart postgresql。