我已启用登录postgres。它在CSV中登录。第九列是实际执行的查询。我想运行一个cut
命令来提取第9列,但问题是查询中的逗号和换行符会破坏解析器。有什么方法可以更改分隔符和换行符吗?
最佳答案
如PostgreSQL Manual中所建议的,您可以使用postgres解析自己的日志:
创建临时表;
导入日志文件;
选择所需数据。
这里有一个简单的bash脚本来自动化这个过程。
#!/usr/bin/env bash
psql -t << EOF
CREATE TEMPORARY 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,
PRIMARY KEY (session_id, session_line_num)
);
COPY postgres_log FROM '$1' WITH csv;
SELECT query from postgres_log;
EOF
您可以将其运行为:
./fetch-queries.sh /full/path/to/logfile.csv
关于bash - 在Postgres日志记录机制中更改定界符和换行符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34398442/