最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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 ?

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 ?

Share Improve this question asked Feb 7 at 19:50 Peter Van BiesenPeter Van Biesen 111 silver badge2 bronze badges New contributor Peter Van Biesen is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

1 Answer 1

Reset to default 1

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.

发布评论

评论列表(0)

  1. 暂无评论