简体   繁体   English

psql:从文件加载查询并将 output psql 消息加载到日志文件

[英]psql : load query from file and output psql messages to log file

I'm trying to find a one-liner to read query from external file and output psql NOTICEs to a log file.我试图找到一个单行代码来从外部文件和 output psql NOTICE 读取查询到日志文件。 I tried:我试过了:

psql -h host -d database -U username -f my_sql_file.sql > my_log_file.log

my_sql_file.sql has a multiline anonymous block like: my_sql_file.sql 有一个多行匿名块,如:

DO
$$
DECLARE
  stuff 
BEGIN
   RAISE NOTICE '--------- TEST ----------';
END;
$$

The log file outputs the first line of the file: "DO" and nothing else.日志文件输出文件的第一行:“DO”,仅此而已。

The notice gets written to standard error, so redirect that too:该通知被写入标准错误,因此也将其重定向:

psql -h host -d database -U username -f my_sql_file.sql > my_log_file.log 2>&1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM