psql > split > queries & output

Started by Wim Bertelsover 5 years ago6 messagesgeneral
Jump to latest
#1Wim Bertels
wim.bertels@ucll.be

Hello,

psql shows (the queries and) the output,
there are for example the -L option and echo_queries option

is there a way to run psql so that:
* inside psql there are no surprises :) (ignore this line:)
* one pipe (or..) to a file which only contains the statements
(queries) history (and not the output)
* another pipe (or..) to a file which only contains the output (with or
without the statements) history

why? for example,
to have easy access in a psql demonstration to past commands
(history),
without the output cluttering or overwhelming everything, side by side
on the screen. (optionally with a third pane for the output)

there is also the /s option in psql,
but i guess the history is only written to file when psql is closed,
and is not accessible during the session (only in memory?),
otherwise this would also be an option
(not ctrl+r)

suggestions or alternatives while using the commandline?

--
mvg,
Wim
--
https://coronalert.be

--
Always do right. This will gratify some people and astonish the rest.
-- Mark Twain

#2Pavel Stehule
pavel.stehule@gmail.com
In reply to: Wim Bertels (#1)
Re: psql > split > queries & output

čt 3. 12. 2020 v 12:39 odesílatel Wim Bertels <wim.bertels@ucll.be> napsal:

Hello,

psql shows (the queries and) the output,
there are for example the -L option and echo_queries option

is there a way to run psql so that:
* inside psql there are no surprises :) (ignore this line:)
* one pipe (or..) to a file which only contains the statements
(queries) history (and not the output)
* another pipe (or..) to a file which only contains the output (with or
without the statements) history

why? for example,
to have easy access in a psql demonstration to past commands
(history),
without the output cluttering or overwhelming everything, side by side
on the screen. (optionally with a third pane for the output)

there is also the /s option in psql,
but i guess the history is only written to file when psql is closed,
and is not accessible during the session (only in memory?),
otherwise this would also be an option
(not ctrl+r)

suggestions or alternatives while using the commandline?

Unfortunately it is not possible. Both contents are sent to stdout. The
own hack can be pretty easy, but without it it is not possible.

Regards

Pavel

Show quoted text

--
mvg,
Wim
--
https://coronalert.be

--
Always do right. This will gratify some people and astonish the rest.
-- Mark Twain

#3Daniel Verite
daniel@manitou-mail.org
In reply to: Wim Bertels (#1)
Re: psql > split > queries & output

Wim Bertels wrote:

* one pipe (or..) to a file which only contains the statements
(queries) history (and not the output)

* another pipe (or..) to a file which only contains the output (with or
without the statements) history

Consider that script:

$ cat f.sql
\set ECHO queries
\o output.txt
select 1;
select 2;
select 3;

If it's run with

$ psql -f f.sql >queries.txt

then it produces the queries in queries.txt and the output in output.txt

Best regards,
--
Daniel Vérité
PostgreSQL-powered mailer: https://www.manitou-mail.org
Twitter: @DanielVerite

#4Wim Bertels
wim.bertels@ucll.be
In reply to: Daniel Verite (#3)
Re: psql > split > queries & output

Daniel Verite schreef op do 03-12-2020 om 15:18 [+0100]:

Wim Bertels wrote:

* one pipe (or..) to a file which only contains the statements
(queries) history (and not the output)
* another pipe (or..) to a file which only contains the output
(with or
without the statements) history

Consider that script:

$ cat f.sql
\set ECHO queries
\o output.txt
select 1;
select 2;
select 3;

If it's run with

$ psql -f f.sql >queries.txt

then it produces the queries in queries.txt and the output in
output.txt

thank you,
i use the same with a input file,
but interactively this doesn't seem to work

ie
psql > queries.txt (does not work (stdout))
(psql 2> queries.txt does work (stderr))
(psql &> queries.txt does not work (both))

--

psql -f - > out

does work interactively,
but no fun experience :) (no completion, and the separation does not
seem to work)

Best regards,

--
mvg,
Wim
--
https://coronalert.be

--
If you laid all of our laws end to end, there would be no end.
-- Mark Twain

#5Daniel Verite
daniel@manitou-mail.org
In reply to: Wim Bertels (#4)
Re: psql > split > queries & output

Wim Bertels wrote:

but interactively this doesn't seem to work

Interactively something like this might do the job:

$ PSQL_HISTORY=/tmp/queries.txt psql -L /tmp/logfile.txt

After running some queries, and a clean exit from psql:

$ cat /tmp/queries.txt
select 1;
select 2;

$ cat /tmp/logfile.txt
********* QUERY **********
select 1;
**************************

?column?
----------
1
(1 row)

********* QUERY **********
select 2;
**************************

?column?
----------
2
(1 row)

Best regards,
--
Daniel Vérité
PostgreSQL-powered mailer: https://www.manitou-mail.org
Twitter: @DanielVerite

#6Wim Bertels
wim.bertels@ucll.be
In reply to: Daniel Verite (#5)
Re: psql > split > queries & output

Daniel Verite schreef op do 03-12-2020 om 16:32 [+0100]:

Wim Bertels wrote:

but interactively this doesn't seem to work

Interactively something like this might do the job:

$ PSQL_HISTORY=/tmp/queries.txt psql -L /tmp/logfile.txt

After running some queries, and a clean exit from psql:

yes indeed,
but as i mentioned in the beginning,
i can't consult the current history in a side pane,
only with option /s
(while still running the psql session)

(side note:
$ echo $PSQL_HISTORY (is empty)

$ psql
psql (12.5 (Debian 12.5-1.pgdg100+1))
Type "help" for help.
# \set HISTFILE /tmp/test
# select 1;

has no effect, /tmp/test is not created,
everything is put into ~.psql_history )

(debian specific?, intended?, or bug?)

$ cat /tmp/queries.txt
select 1;
select 2;

$ cat /tmp/logfile.txt
********* QUERY **********
select 1;
**************************

?column?
----------
1
(1 row)

********* QUERY **********
select 2;
**************************

?column?
----------
2
(1 row)

Best regards,

--
mvg,
Wim
--
https://coronalert.be

--
In India, "cold weather" is merely a conventional phrase and has come into
use through the necessity of having some way to distinguish between weather
which will melt a brass door-knob and weather which will only make it mushy.
-- Mark Twain