Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

postgresql - Printing to screen in .sql file postgres

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.

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

Is this possible in Postgres?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...