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-08 00:39:02
Message-ID: CA+wokJ9KVasnvZfnims-fmBgoLN012cuo_0Tftez4r1szpaALA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

Awesome, thanks for the info!

On Thu, Feb 8, 2024 at 9:46 AM Erik Wienhold <ewie(at)ewie(dot)name> wrote:

> 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

Browse pgsql-admin by date

  From Date Subject
Next Message Gambhir Singh 2024-02-08 10:30:26 Need help to implement Primary Key.
Previous Message Erik Wienhold 2024-02-07 20:46:41 Re: How to output psql to file AND screen/console