记录 PostgreSQL 交互式查询
Logging PostgreSQL interactive queries
我有几个人直接在某些系统中进行一些数据库查询。
我想将 interactively/by 手动完成的所有查询记录到安全的系统日志服务器,否则,使用 psql
二进制客户端完成的所有查询。
我正在使用 Debian Jessie、PostgreSQL 9.4 和 9.1。
我怎样才能做到这一点?
设置配置变量有a multitude of methods个,范围不同。
由于 log_statement
是仅限超级用户的设置,您可以排除所有客户端选项,并且服务器或数据库范围的设置可能对您的情况来说过于宽泛。
我相信这只会给您留下每个用户的选项,即:
ALTER USER interactive_user SET log_statement = 'all';
或者如果它使事情更易于管理:
ALTER SYSTEM SET log_statement = 'all';
ALTER USER application_user SET log_statement = 'none';
请注意,这些设置仅适用于连接,因此无法通过 SET ROLE
命令触发(或绕过)它们。
当然,这一切都假设您的应用程序和您的交互式用户不共享登录名,但我认为没有其他任何具有正确粒度级别的东西。
您还可以将这些指令添加到通常位于 /etc/postgresql/{version}/main/postgresql.conf
或 /etc/postgresql/{version}/main/conf.d/my-overrides.conf
中的配置文件中,例如
log_connections = true
log_disconnections = true
log_statement = all
log_duration = true
您还可以添加 log_line_prefix
以提供有关连接 (from the documentation) 的更多详细信息,格式为:
log_line_prefix = "string"
例如
log_line_prefix = '%m [%p-%l] [%c] %u %d %a '
printf
选项在文档中(为了完整起见)是:
%a Application name
%u User name
%d Database name
%r Remote host name or IP address, and remote port
%h Remote host name or IP address
%p Process ID
%t Time stamp without milliseconds
%m Time stamp with milliseconds
%n Time stamp with milliseconds (as a Unix epoch)
%i Command tag: type of session's current command
%e SQLSTATE error code
%c Session ID: see below
%l Number of the log line for each session or process, starting at 1
%s Process start time stamp
%v Virtual transaction ID (backendID/localXID)
%x Transaction ID (0 if none is assigned)
%q Produces no output, but tells non-session processes to stop at this point in the string; ignored by session processes
我有几个人直接在某些系统中进行一些数据库查询。
我想将 interactively/by 手动完成的所有查询记录到安全的系统日志服务器,否则,使用 psql
二进制客户端完成的所有查询。
我正在使用 Debian Jessie、PostgreSQL 9.4 和 9.1。
我怎样才能做到这一点?
设置配置变量有a multitude of methods个,范围不同。
由于 log_statement
是仅限超级用户的设置,您可以排除所有客户端选项,并且服务器或数据库范围的设置可能对您的情况来说过于宽泛。
我相信这只会给您留下每个用户的选项,即:
ALTER USER interactive_user SET log_statement = 'all';
或者如果它使事情更易于管理:
ALTER SYSTEM SET log_statement = 'all';
ALTER USER application_user SET log_statement = 'none';
请注意,这些设置仅适用于连接,因此无法通过 SET ROLE
命令触发(或绕过)它们。
当然,这一切都假设您的应用程序和您的交互式用户不共享登录名,但我认为没有其他任何具有正确粒度级别的东西。
您还可以将这些指令添加到通常位于 /etc/postgresql/{version}/main/postgresql.conf
或 /etc/postgresql/{version}/main/conf.d/my-overrides.conf
中的配置文件中,例如
log_connections = true
log_disconnections = true
log_statement = all
log_duration = true
您还可以添加 log_line_prefix
以提供有关连接 (from the documentation) 的更多详细信息,格式为:
log_line_prefix = "string"
例如
log_line_prefix = '%m [%p-%l] [%c] %u %d %a '
printf
选项在文档中(为了完整起见)是:
%a Application name
%u User name
%d Database name
%r Remote host name or IP address, and remote port
%h Remote host name or IP address
%p Process ID
%t Time stamp without milliseconds
%m Time stamp with milliseconds
%n Time stamp with milliseconds (as a Unix epoch)
%i Command tag: type of session's current command
%e SQLSTATE error code
%c Session ID: see below
%l Number of the log line for each session or process, starting at 1
%s Process start time stamp
%v Virtual transaction ID (backendID/localXID)
%x Transaction ID (0 if none is assigned)
%q Produces no output, but tells non-session processes to stop at this point in the string; ignored by session processes