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

From: Erik Wienhold <ewie(at)ewie(dot)name>
To: Edwin UY <edwin(dot)uy(at)gmail(dot)com>
Cc: pgsql-admin(at)lists(dot)postgresql(dot)org
Subject: Re: How to output psql to file AND screen/console
Date: 2024-02-07 20:46:41
Message-ID: 5wkcn5hggaybmfhpkqew2j2us524wdzem4a7lzn23mgu4i6szz@trur3wb5zucx
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

On 2024-02-07 20:13 +0100, Edwin UY wrote:
> Sorry I didn't mention I am running the script as \i [sqlfile]
>
> More often than not, I do a \conninfo first to confirm which database I am
> connecting to just to be safe. Wanting to redirect that information to the
> output file is for reference in case there's an issue and I needed to prove
> that I am indeed connected to the right database/host based on \conninfo.

You can also use a wrapper script that runs \conninfo before prompting
the user on whether to continue with the actual script or abort. For
example:

\conninfo
\prompt 'continue? [yes/no] ' continue
\if :continue
\echo 'continue'
\unset continue
\i :script
\else
\echo 'abort'
\quit
\endif

Run the wrapper by passing the script filename as variable :script:

$ psql -v script=myscript.sql -f wrapper.sql | tee outfile

> At the moment, using script [outfile] and \o | tee [outfile]. The latter
> produces a cleaner output. Will search if there is a SQL that will give a
> similar output to \conninfo or do you happen to have one :-)

There's an active thread on -hackers about extending \conninfo. There
you'll find a query[1] with functions that provide the same info.

[1] https://www.postgresql.org/message-id/20240206210605.GA3903769%40nathanxps13

--
Erik

In response to

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Edwin UY 2024-02-08 00:39:02 Re: How to output psql to file AND screen/console
Previous Message Edwin UY 2024-02-07 19:13:53 Re: How to output psql to file AND screen/console