将用户、IP 地址添加到 PostgreSQL 日志记录

Ham*_*one 2 postgresql logging

我假设我在文档中遗漏了一些简单的内容,但在我的一生中,我无法弄清楚如何在 PostgreSQL 的日志文件中添加运行查询的机器的用户 ID 和 IP 地址。pg_stat_activity一旦发生,我可以清楚地看到它,但是在对数据库问题进行事后分析时,在日志中看到这些会非常有帮助。

我怀疑这很重要,但我的服务器启动是这样的:

$BIN/pg_ctl -D /apps/pgdata9.5.0 -l /apps/logs/postgresql start
Run Code Online (Sandbox Code Playgroud)

我希望在 postgresql.conf 中有一个设置,但如果在那里它就躲过了我。

Vao*_*sun 5

https://www.postgresql.org/docs/current/static/runtime-config-logging.html

添加%u %hlog_line_prefixpostgres.conf 中

上面的文档中有一个带有其他选项的表格

更新:

查询pg_settings.context以检查是否需要重新启动 https://www.postgresql.org/docs/current/static/view-pg-settings.html

设置参数值所需的上下文(见下文)

例如:

t=# select context from pg_settings where name = 'log_line_prefix';
 context
---------
 sighup
Run Code Online (Sandbox Code Playgroud)

  • @Hambone:您不需要重新启动服务器。只需重新加载配置文件,例如`select pg_reload_conf()`:https://www.postgresql.org/docs/current/static/functions-admin.html#FUNCTIONS-ADMIN-SIGNAL (3认同)