Re: How to output psql to file AND screen/console

From: Edwin UY <edwin(dot)uy(at)gmail(dot)com>
To: Erik Wienhold <ewie(at)ewie(dot)name>
Cc: pgsql-admin(at)lists(dot)postgresql(dot)org
Subject: Re: How to output psql to file AND screen/console
Date: 2024-02-07 10:39:36
Message-ID: CA+wokJ8BXiCkZtoy0nPu-4arROWvK=-DvMpmCS6s2GM6t2D3CQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

Thanks Erik, the \o | tee outfile does the trick. Never thought of that.
It appears to exclude the output from psql I think like \conninfo or
\encoding.
Is there a way to get those to show in the outfile too? This is the same
for when not using tee as well.

On Wed, Feb 7, 2024 at 11:11 PM Erik Wienhold <ewie(at)ewie(dot)name> wrote:

> On 2024-02-07 10:16 +0100, Edwin UY wrote:
> > Is there any way for psql to output to the console AND file at the same
> > time?
> > \o redirect the output to the file but at the same time it suppresses the
> > output to the console :(
> >
> > I am wanting to redirect the output to a file for the psql session for
> the
> > SQL/SQL-script that I ran while at the same see the same output to
> screen.
> >
> > Thought I can use tee -a but I can't :(
>
> Why not? That was my first idea.
>
> $ psql | tee outfile
>
> But the psql prompt is not printed on the console and tab completion via
> readline does not work. Maybe I'm missing something.
>
> What does work is using \o and redirect to tee:
>
> $ psql finance
> psql (16.1)
> Type "help" for help.
>
> postgres=# \o | tee outfile
> postgres=# select now();
> now
> -------------------------------
> 2024-02-07 11:08:23.207647+01
> (1 row)
>
> postgres=# \q
>
> $ cat outfile
> now
> -------------------------------
> 2024-02-07 11:08:23.207647+01
> (1 row)
>
> > Using script [outputfile] seems to work. Just thought maybe there is any
> > other psql trick besides using script. The script has lots of control
> > characters :(
>
> --
> Erik
>

In response to

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Erik Wienhold 2024-02-07 12:52:40 Re: How to output psql to file AND screen/console
Previous Message Erik Wienhold 2024-02-07 10:11:10 Re: How to output psql to file AND screen/console