postgresql - 带有逗号分隔列的自定义输出文件

postgresql - customized output file with comma separated columns

我们使用的是 postgresql 8/psql 8.4 版。我正在查询 information_schema.columns 列名,并想生成一个文本 file/output 文件,因此:

UNLOAD ('select 
col1,
col2,
col3,
col4,
col5)
to more text here

UNLOAD ( 'select col1,col2,col3,col4,col5) to more text here

所以,我基本上是想输出 colname 后跟 "," - colname,

这可能吗?感谢您的帮助。

这将创建一个类似这样的字符串:

SELECT  'UNLOAD ( ''select ' ||
        array_to_string(array_agg(column_name::text), ',') ||
        ' to more text here'
FROM    information_schema.columns
WHERE   table_schema = 'public'
AND     table_name = 'whatever'
;

您可以使用\o将其发送到文件,或使用COPY (...) TO STDOUT