如何停止psql(PostgreSQL客户端)输出通知? e.g。
psql:schema / auth.sql:20:注意:CREATE TABLE / PRIMARY KEY将为表“users”创建隐式索引“users_pkey”
在我看来,除非出现错误或输出内容的其他原因,否则程序应该保持沉默。
答案 0 :(得分:57)
SET client_min_messages TO WARNING;
That只能为会话设置,或者通过ALTER ROLE
或ALTER DATABASE
保持不变。
或者您可以将其放在".psqlrc"。
中答案 1 :(得分:48)
可能最全面的解释是关于Peter Eisentrauts blog entry here
我强烈建议对原始博客进行研究和消化,但最终建议如下:
PGOPTIONS='--client-min-messages=warning' psql -X -q -a -1 -v ON_ERROR_STOP=1 --pset pager=off -d mydb -f script.sql
答案 2 :(得分:19)
启动psql时使用--quiet
。
通知并非无用,但这是我的观点。
答案 3 :(得分:1)
提供对我遇到的特定场景有用的建议:
示例:
psql.exe -c "SET client_min_messages TO WARNING; DROP TABLE IF EXISTS mytab CASCADE"
(我无法将 PGOPTIONS 用作 Windows 环境变量——无法计算出正确的语法。尝试了来自不同帖子的多种方法。)
答案 4 :(得分:0)
可以在全局postgresql.conf
文件中设置,也可以修改client_min_messages
参数。
示例:
client_min_messages = warning
答案 5 :(得分:0)
我尝试了此线程中建议的各种解决方案(及其排列),但无法完全禁止PSQL输出/通知。
我正在执行一个claws2postgres.sh
BASH脚本,该脚本进行一些初步处理,然后调用/执行PSQL .sql脚本,以将1000个条目插入PostgreSQL。
...
PGOPTIONS="-c client_min_messages=error"
psql -d claws_db -f claws2postgres.sql
输出
[victoria@victoria bash]$ ./claws2postgres.sh
pg_terminate_backend
----------------------
DROP DATABASE
CREATE DATABASE
You are now connected to database "claws_db" as user "victoria".
CREATE TABLE
SELECT 1
INSERT 0 1
UPDATE 1
UPDATE 1
UPDATE 1
Dropping tmp_table
DROP TABLE
You are now connected to database "claws_db" as user "victoria".
psql:/mnt/Vancouver/projects/ie/claws/src/sql/claws2postgres.sql:33: NOTICE: 42P07: relation "claws_table" already exists, skipping
LOCATION: transformCreateStmt, parse_utilcmd.c:206
CREATE TABLE
SELECT 1
INSERT 0 1
UPDATE 2
UPDATE 2
UPDATE 2
Dropping tmp_table
DROP TABLE
[ ... snip ... ]
解决方案
请注意以下经过修改的PSQL行,在此我将psql输出重定向:
psql -d claws_db -f $SRC_DIR/sql/claws2postgres.sql &>> /tmp/pg_output.txt
&>> /tmp/pg_output.txt
重定向将所有输出附加到一个输出文件,该文件也可以用作日志文件。
BASH终端输出
[victoria@victoria bash]$ time ./claws2postgres.sh
pg_terminate_backend
----------------------
DROP DATABASE
CREATE DATABASE
2:40:54 ## 2 h 41 min
[victoria@victoria bash]$
监控进度:
在另一个终端中,执行
PID=$(pgrep -l -f claws2postgres.sh | grep claws | awk '{ print $1 }'); while kill -0 $PID >/dev/null 2>&1; do NOW=$(date); progress=$(cat /tmp/pg_output.txt | wc -l); printf "\t%s: %i lines\n" "$NOW" $progress; sleep 60; done; for i in seq{1..5}; do aplay 2>/dev/null /mnt/Vancouver/programming/scripts/phaser.wav && sleep 0.5; done
...
Sun 28 Apr 2019 08:18:43 PM PDT: 99263 lines
Sun 28 Apr 2019 08:19:43 PM PDT: 99391 lines
Sun 28 Apr 2019 08:20:43 PM PDT: 99537 lines
[victoria@victoria output]$
pgrep -l -f claws2postgres.sh | grep claws | awk '{ print $1 }'
获取分配给$ PID的脚本PID while kill -0 $PID >/dev/null 2>&1; do ...
:在脚本运行时,执行... cat /tmp/pg_output.txt | wc -l
:将输出文件行数用作进度指示器phaser.wav
5次来通知输出文件:
[victoria@victoria ~]$ head -n22 /tmp/pg_output.txt
You are now connected to database "claws_db" as user "victoria".
CREATE TABLE
SELECT 1
INSERT 0 1
UPDATE 1
UPDATE 1
UPDATE 1
Dropping tmp_table
DROP TABLE
You are now connected to database "claws_db" as user "victoria".
psql:/mnt/Vancouver/projects/ie/claws/src/sql/claws2postgres.sql:33: NOTICE: 42P07: relation "claws_table" already exists, skipping
LOCATION: transformCreateStmt, parse_utilcmd.c:206
CREATE TABLE
SELECT 1
INSERT 0 1
UPDATE 2
UPDATE 2
UPDATE 2
Dropping tmp_table
DROP TABLE
参考
[相关的SO线程] Postgresql - is there a way to disable the display of INSERT statements when reading in from a file?
[与解决方案有关] https://askubuntu.com/questions/350208/what-does-2-dev-null-mean
The > operator redirects the output usually to a file but it can be to a device. You can also use >> to append.
If you don't specify a number then the standard output stream is assumed but you can also redirect errors
> file redirects stdout to file
1> file redirects stdout to file
2> file redirects stderr to file
&> file redirects stdout and stderr to file
/dev/null is the null device it takes any input you want and throws it away. It can be used to suppress any output.