如何从命令行以 CSV 或 TSV 格式打印 PostgreSQL 查询的结果?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6521531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 22:59:22  来源:igfitidea点击:

How do you print the result of a PostgreSQL query in CSV or TSV format from the command line?

postgresql

提问by dan

I'd like to execute a query from the shell (not in the interactive psql client) and have it print the CSV or TSV representation of the output to STDOUT. How do you do that with psqlor one of the PostgreSQL command-line tools?

我想从 shell(而不是在交互式 psql 客户端中)执行查询,并让它将输出的 CSV 或 TSV 表示打印到 STDOUT。您如何使用psql或其中一种 PostgreSQL 命令行工具来做到这一点?

回答by Matthew Wood

If you are using PostgreSQL 8.2 or newer, use this for CSV:

如果您使用的是 PostgreSQL 8.2 或更高版本,请将其用于 CSV:

psql -c "COPY (<select query>) TO STDOUT WITH CSV"

and this of TSV, with proper NULLs:

和这个 TSV,带有适当的 NULL:

psql -c "COPY (<select query>) TO STDOUT WITH NULL AS ''"

The CSV form will properly quote any fields that contain the double-quote character. See the PostgreSQL documentation of your specific version for more details and options for COPY.

CSV 表单将正确引用包含双引号字符的任何字段。有关 COPY 的更多详细信息和选项,请参阅特定版本的 PostgreSQL 文档。

回答by Jason McVetta

Starting from Bohemian's answer, I found these flags useful:

从 Bohemian 的回答开始,我发现这些标志很有用:

psql my_database -U myuser -A -F , -X -t -f /path/to/query.sql -o /path/to/output.csv
  • Unaligned output mode: -A
  • Use comma as field delimiter: -F ,
  • Do not read psqlrc: -X
  • Tuples only (no header/footer): -t
  • File containing SQL query: -f
  • Output file: -o
  • 未对齐输出模式:-A
  • 使用逗号作为字段分隔符: -F ,
  • 不读 psqlrc: -X
  • 仅元组(无页眉/页脚):-t
  • 包含 SQL 查询的文件:-f
  • 输出文件:-o

回答by Bohemian

EDITED: Using -F

编辑:使用 -F

Use commas via -Fand use "unaligned table output mode" -A:

使用逗号 via-F并使用“未对齐的表输出模式” -A

psql my_database -U myuser -A -F , -c "select * from mytable"

回答by ConKat

To specify a tsvuse the delimiter '\t'

要指定tsv,请使用分隔符 '\t'

psql my_database -U myuser -F'\t' --no-align -f mysqlfile.sql -o outputfile.tsv

To specify a csvuse the delimiter ','

要指定csv,请使用分隔符 ','

psql my_database -U myuser -F',' --no-align -f mysqlfile.sql -o outputfile.csv

回答by Gavin

Also possible is the copycommand which allows you to specify header, delimiters and quoting options

也可能是复制命令,它允许您指定标题、分隔符和引用选项

psql my_database -U my_user -c "copy (select a.id,b.id from my_table_a as a inner join my_table_b as b on b.id = a.id) to STDOUT"

回答by Szocske

You can specify the field separator with the -F command line parameter to psql

您可以使用 -F 命令行参数为 psql 指定字段分隔符

回答by Yan Foto

The simplest way (using psql) seems to be by using --csvflag:

最简单的方法(使用psql)似乎是使用--csv标志:

psql --csv -c "SELECT * FROM sometable"

回答by Victoria Stuart

Export AS TSV WITH HEADER

导出为带标题的 TSV

You can include the HEADER as follows:

您可以按如下方式包含 HEADER:

\COPY (SELECT * FROM tca) TO '/.../metab/tca.tsv' WITH DELIMITER E'\t' CSV HEADER;

\COPY (SELECT * FROM tca) TO '/...a/metab/tca.tsv' WITH NULL AS '' DELIMITER E'\t' CSV HEADER;

E.g. (PSQL):

例如(PSQL):

[metabolism]# \COPY (SELECT * FROM tca) TO '/mnt/Vancouver/programming/data/metabolism/tca.tsv' WITH NULL AS '' DELIMITER E'\t' CSV HEADER;
COPY 22

BASH:

巴什:

[victoria@victoria tsv]$ pwd
/mnt/Vancouver/programming/data/metabolism/tsv

[victoria@victoria tsv]$ head -n3 tca.tsv
uuid    src tgt rel rel_type
878b87de-0ca8-49a8-9f77-a24353e251d2    oxalosuccinic acid  oxoglutaric acid    1.1.1.42    2
7fd9cf88-495b-491b-956e-294f19097923    isocitric acid  oxoglutaric acid    1.1.1.41    2
[victoria@victoria csv]$