Re: How to output psql to file AND screen/console - Mailing list pgsql-admin

From Erik Wienhold
Subject Re: How to output psql to file AND screen/console
Date
Msg-id 5wkcn5hggaybmfhpkqew2j2us524wdzem4a7lzn23mgu4i6szz@trur3wb5zucx
Whole thread Raw
In response to Re: How to output psql to file AND screen/console  (Edwin UY <edwin.uy@gmail.com>)
Responses Re: How to output psql to file AND screen/console
List 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



pgsql-admin by date:

Previous
From: Edwin UY
Date:
Subject: Re: How to output psql to file AND screen/console
Next
From: Edwin UY
Date:
Subject: Re: How to output psql to file AND screen/console