PostgreSQL 禁用更多输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11180179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PostgreSQL disable more output
提问by Chris
I am running a script on my PostgreSQL server:
我在我的 PostgreSQL 服务器上运行一个脚本:
psql db -f sql.sql
from bash
or in a cron
script.
来自bash
或在cron
脚本中。
It keeps trying to paginate the output with more
or less
.
它不断尝试使用more
or对输出进行分页less
。
How do I disable result pagination in psql
?
如何禁用结果分页psql
?
All I want to do is change the data, I don't care about any output.
我想做的就是改变数据,我不在乎任何输出。
回答by Craig Ringer
To disable pagination but retainthe output, use:
要禁用分页但保留输出,请使用:
\pset pager off
To remember this setting, add it to your ~/.psqlrc.
要记住此设置,请将其添加到您的~/.psqlrc 中。
See the psql manual.
请参阅psql 手册。
On older versions of Pg it was just a toggle, so \pset pager
在旧版本的 Pg 上它只是一个切换,所以 \pset pager
To completely suppress query output, use \o /dev/null
in your psql
script.
要完全抑制查询输出,请\o /dev/null
在psql
脚本中使用。
To suppress psql
's informational output, run it with -q
or set QUIET=1
in the environment.
要抑制psql
的信息输出,请在环境中使用-q
或设置运行它QUIET=1
。
To produce results and throw them away you can redirect stdout
to /dev/null
with:
要产生结果并将它们扔掉,您可以重定向stdout
到/dev/null
:
psql db -f sql.sql >/dev/null
You can redirect both stdout and stderr with:
您可以使用以下命令重定向 stdout 和 stderr:
psql db -f sql.sql >&/dev/null
but I don't recommend that, as it'll throw away error information that might warn you something isn't going right. You're also producing results and throwing them away, which is inefficient; you're better off just not producing them in the first place by adjusting your queries.
但我不建议这样做,因为它会丢弃错误信息,这些信息可能会警告您出现问题。你也在产生结果并将它们扔掉,这是低效的;您最好不要通过调整查询来首先生成它们。
回答by Davide
I was looking for this too, I found the way in a similar questionon ServerFault:
我也在寻找这个,我在 ServerFault 上的一个类似问题中找到了方法:
psql -P pager=off <other params>
turns off the paging thing, without suppressing output.
关闭分页功能,而不抑制输出。
回答by FMc
Here's another option. It does have the advantage that you don't have to remember psql option names, etc.
这是另一种选择。它的优点是您不必记住 psql 选项名称等。
psql ... | cat
回答by Harald Brinkhof
bash, being a shell, has 2 streamsyou can redirect that output data: stdout and stderr, because this output needs to be redirected somewhere, linux has a specific 'discard everything' node reachable through /dev/null. Everything you send there will just disappear into the void.
作为shell 的bash有 2 个流,您可以重定向输出数据:stdout 和 stderr,因为此输出需要重定向到某个地方,linux 有一个特定的“丢弃所有内容”节点,可通过/dev/null 访问。你发送到那里的所有东西都会消失在虚空中。
(shells also have an input stream but I'll ignore this here since you asked for suppressing output)
(shell 也有一个输入流,但我会在这里忽略这个,因为你要求抑制输出)
These streams are represented by numbers: 1 for stdout and 2 for stderr.
这些流由数字表示:1 代表标准输出,2 代表标准错误。
So if you want to redirect just stdout you'd do that with the <
and >
operators (basically where it points to is where the data flows to)
因此,如果您只想重定向 stdout,您可以使用<
and>
操作符(基本上它指向的位置是数据流向的位置)
suppose we want to suppress stdout (redirect to /dev/null):
假设我们想抑制 stdout(重定向到 /dev/null):
psql db -f sql.sql > /dev/null
psql db -f sql.sql > /dev/null
As you can see this is stdout is default, no stream number has been used if you wanted to use the stream number you'd write
正如你所看到的,这是默认的标准输出,如果你想使用你写的流号,没有使用流号
psql db -f sql.sql 1> /dev/null
psql db -f sql.sql 1> /dev/null
Now if you want to suppress stderror (stream number 2), you'd use
现在,如果您想抑制 stderror(流编号 2),您可以使用
psql db -f sql.sql 2> /dev/null
psql db -f sql.sql 2> /dev/null
You could also redirect one stream to another, for example stderror to stdout, which is useful if you want to save all output somewhere, regular and errors.
您还可以将一个流重定向到另一个流,例如 stderror 到 stdout,如果您想将所有输出、常规和错误保存在某处,这将非常有用。
psql db -f sql.sql 2>&1 > log.txt
psql db -f sql.sql 2>&1 > log.txt
mind you there can not be spaces between 2>&1
请注意你之间不能有空格 2>&1
Finally and sometimes most interesting is the fact that you can suppress all output by using &>
, for when you want it 'perfectly quiet'
最后,有时也是最有趣的是,您可以通过使用 来抑制所有输出&>
,因为当您希望它“完全安静”时
psql db -f sql.sql &> /dev/null
psql db -f sql.sql &> /dev/null
回答by Marc B
psql db -f sql.sql > /dev/null
回答by Ranjith Reddy
psql -U user -P pager=off -d database -c 'SQL';