postgresql 在 .sql 文件 postgres 中打印到屏幕
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17383056/
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
Printing to screen in .sql file postgres
提问by El Guapo
This sounds like it should be a very easy thing to do, however, I cannot find ANYWHERE how to do it.
这听起来应该是一件非常容易的事情,但是,我无法在任何地方找到如何去做。
I have a .sql file I am building for an upgrade to my application that alters tables, inserts/updates, etc.
我正在构建一个 .sql 文件,用于升级我的应用程序,该文件可以更改表、插入/更新等。
I want to write to the screen after every command finishes.
我想在每个命令完成后写到屏幕上。
So, for instance if I have something like:
所以,例如,如果我有类似的东西:
insert into X...
I want to see something like,
我想看类似的东西
Starting to insert into table X
Finished inserting into table X
开始插入表X
完成插入表X
Is this possible in Postgres?
这在 Postgres 中可能吗?
回答by mu is too short
If you're just feeding a big pile of SQL to psql
then you have a couple options.
如果您只是提供一大堆 SQL,psql
那么您有几个选择。
You could run psql
with --echo-all
:
您可以运行psql
使用--echo-all
:
-a
--echo-all
Print all input lines to standard output as they are read. This is more useful for script processing than interactive mode. This is equivalent to setting the variableECHO
toall
.
-a
--echo-all
在读取时将所有输入行打印到标准输出。这对于脚本处理比交互模式更有用。这相当于将变量设置ECHO
为all
。
That and the other "echo everything of this type" options (see the manual) are probably too noisy though. If you just want to print things manually, use \echo
:
那个和其他“回显这种类型的所有内容”选项(参见手册)可能太吵了。如果您只想手动打印内容,请使用\echo
:
\echo
text
[ ... ]
Prints the arguments to the standard output, separated by one space and followed by a newline. This can be useful to intersperse information in the output of scripts.
\echo
text
[ ... ]
将参数打印到标准输出,以一个空格分隔,后跟换行符。这对于在脚本输出中散布信息很有用。
So you can say:
所以你可以说:
\echo 'Starting to insert into table X'
-- big pile of inserts go here...
\echo 'Finished inserting into table X'
回答by shanemgrey
There's probably a better way to do it. But if you need to use vanilla SQL, try this:
可能有更好的方法来做到这一点。但是如果你需要使用 vanilla SQL,试试这个:
SELECT NULL AS "Starting to insert into table X";
-- big pile of inserts go here...
SELECT NULL AS "Finished inserting into table X";
回答by FuriousFolder
Via: https://stackoverflow.com/a/18828523/2014857
通过:https: //stackoverflow.com/a/18828523/2014857
DO language plpgsql $$
BEGIN
RAISE NOTICE 'hello, world!';
END
$$;
Depending on what you're doing, I'd be worried about doing a bunch of anonymous code blocks. You might consider storing the above as a function, and passing in whatever value you want logged.
根据你在做什么,我会担心做一堆匿名代码块。您可能会考虑将上述内容存储为一个函数,并传入您想要记录的任何值。