在postgres中导出为CSV并使用GZIP压缩 [英] Export to CSV and Compress with GZIP in postgres

查看:214
本文介绍了在postgres中导出为CSV并使用GZIP压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将一个大表导出到csv文件并进行压缩.

I need to export a big table to csv file and compress it.

我可以使用COPY命令从类似-

I can export it using COPY command from postgres like -

COPY foo_table to '/tmp/foo_table.csv' delimiters',' CSV HEADER;

然后可以使用gzip压缩它-

And then can compress it using gzip like -

gzip -c foo_table.csv > foo.gz

这种方法的问题是,我需要在获得最终的压缩文件之前创建一个本身很大的中间csv文件.

The problem with this approach is, I need to create this intermediate csv file, which itself is huge, before I get my final compressed file.

有没有一种方法可以在csv中导出表并一步压缩文件?

Is there a way of export table in csv and compressing the file in one step?

关于, 苏吉特

推荐答案

诀窍是使COPY将其输出发送到stdout,然后通过gzip将输出通过管道传递:

The trick is to make COPY send its output to stdout, then pipe the output through gzip:

psql -c "COPY foo_table TO stdout DELIMITER ',' CSV HEADER" \
    | gzip > foo_table.csv.gz

这篇关于在postgres中导出为CSV并使用GZIP压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆