简体   繁体   English

将psql输出保存到csv文件

[英]Saving psql output to csv file

I have a query written in a file located at /path/to/query. 我有一个查询写在位于/ path / to / query的文件中。 How can I save the output result to a csv file, without using COPY in the query? 如何在不在查询中使用COPY的情况下将输出结果保存到csv文件? I tried the following command, but the output file's fields are separated by " | ". 我尝试了以下命令,但输出文件的字段用“|”分隔。

psql -U username -d dbname -f /path/to/query -o /path/to/output/file -F ','

It is not explained in the documentation, but the -F option requires the -A option (unaligned table output) to work: 文档中没有解释,但-F选项需要-A选项(未对齐表输出)才能工作:

psql -U username -d dbname -f /path/to/query -o /path/to/output/file -F ',' -A

If you don't wish the headers in your csv, this means, without extra rows at the top and at the bottom, use the -t option too. 如果你不希望你的csv中的标题,这意味着,在顶部和底部没有额外的行,也使用-t选项。

psql -U username -d dbname -f /path/to/query -o /path/to/output/file -F ',' -A -t

From the help: 从帮助:

-A, --no-align unaligned table output mode -A, - no-align未对齐表输出模式
-F, --field-separator=STRING set field separator (default: "|") -F, - field-separator = STRING设置字段分隔符(默认值:“|”)
-t, --tuples-only print rows only -t, - 仅限 - 仅打印行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM