我最近升级到 Postgresql 9.6 和 PgAdmin 4。
现在,PgAdmin 3 的一个方便的功能是“服务器状态”窗口,我过去常常用它查看日志文件。
我在 PgAdmin 4 中找不到。如何在 PgAdmin 4 中查看服务器日志?或者有什么好的选择?
我已按照此处的指南使用外部数据包装器,以便我可以通过 SQL 访问日志文件。
首先,创建扩展:
create extension file_fdw;
Run Code Online (Sandbox Code Playgroud)
接下来,设置 postgres.conf 以支持csv 日志记录。本质上,这些设置:
log_destination='csvlog'
logging_collector='on'
log_filename='postgresql.log'
还有以下内容,来自文档:
设置 log_filename 和 log_rotation_age 以为您的日志文件提供一致、可预测的命名方案。这让您可以预测文件名是什么,并知道单个日志文件何时完成并准备好导入。
将 log_rotation_size 设置为 0 以禁用基于大小的日志轮换,因为它使日志文件名难以预测。
将 log_truncate_on_rotation 设置为 on,以便旧日志数据不会与同一文件中的新数据混合。
创建,服务器和表,可以查询日志!
create server logserver FOREIGN DATA WRAPPER file_fdw;
CREATE FOREIGN TABLE postgres_log
(
log_time timestamp(3) with time zone,
user_name text,
database_name text,
process_id integer,
connection_from text,
session_id text,
session_line_num bigint,
command_tag text,
session_start_time timestamp with time zone,
virtual_transaction_id text,
transaction_id bigint,
error_severity text,
sql_state_code text,
message text,
detail text,
hint text,
internal_query text,
internal_query_pos integer,
context text,
query text,
query_pos integer,
location text,
application_name text)
SERVER logserver OPTIONS (filename 'pg_log/postgresql.csv', format 'csv');
select message from postgres_log;
Run Code Online (Sandbox Code Playgroud)