Printing to screen in .sql file postgres

孤者浪人 提交于 2019-11-29 09:04:01

If you're just feeding a big pile of SQL to psql then you have a couple options.

You could run psql with --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 variable ECHO to 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 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.

So you can say:

\echo 'Starting to insert into table X'
-- big pile of inserts go here...
\echo 'Finished inserting into table X'
FuriousFolder

Via: 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.

There's probably a better way to do it. But if you need to use vanilla SQL, try this:

SELECT NULL AS "Starting to insert into table X";
-- big pile of inserts go here...
SELECT NULL AS "Finished inserting into table X";
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!