Peter Van Biesen
Peter Van Biesen

Reputation: 11

How do I capture the output of \conninfo into a variable in postgresql's psql?

I'm writing a psql sql script to generate an sql script. The way I do this, is by writing every ouputline to a temptable and later on \copy that to the output sql file.

I'd like to include the \conninfo output to the header of the file but I can't seem to capture its output into a variable.

What I tried :

\set conninfo \conninfo

\o tempfile.txt
\conninfo
\o
\set conninfo `cat tempfile.txt`

None seem to work, I assume \conninfo writes to STDERR.

Does anybody have a solution ?

Upvotes: 1

Views: 19

Answers (1)

Adrian Klaver
Adrian Klaver

Reputation: 19724

From here:

https://www.postgresql.org/docs/current/app-psql.html#APP-PSQL-VARIABLES

\set con_info 'Connected to ' :DBNAME  ' at '  :PORT ' as ' :USER

\echo :con_info
Connected to test at 5432 as postgres

This uses the built in variables provided in psql to build a connection description.

Upvotes: 1

Related Questions