在 CentOS 上查看 PostgreSQL 日志,通常有以下几种常见方式,具体取决于你的安装方式和配置。
PostgreSQL 的日志位置由配置文件 postgresql.conf 决定,常见位置:
/var/lib/pgsql/<版本>/data/log/
/var/log/postgresql/
查看配置:
grep log_directory /var/lib/pgsql/*/data/postgresql.conf
/var/lib/pgsql/13/data/log/
/var/lib/pgsql/14/data/log/
查看日志:
ls -lh /var/lib/pgsql/14/data/log/
tail -f /var/lib/pgsql/14/data/log/postgresql-*.log
路径通常一致:
/var/lib/pgsql/14/data/log/
如果是 systemd 管理的 PostgreSQL:
journalctl -u postgresql-14
实时查看:
journalctl -u postgresql-14 -f
查看最近 100 行:
journalctl -u postgresql-14 -n 100
查看配置:
grep -E "logging_collector|log_directory|log_filename" \
/var/lib/pgsql/14/data/postgresql.conf
典型配置:
logging_collector = on
log_directory = 'log'
log_filename = 'postgresql-%Y-%m-%d.log'
如果 logging_collector = off,日志只会输出到 stderr(systemd)。
# 查看错误日志
grep ERROR /var/lib/pgsql/14/data/log/*.log
# 查看启动失败原因
journalctl -xe -u postgresql-14
tail -f /var/lib/pgsql/14/data/log/postgresql-Thu.log
或:
journalctl -u postgresql-14 -f
如果你愿意,可以告诉我:
我可以给你更精确的日志路径和命令。