Export to CSV and Compress with GZIP in postgres

前端 未结 3 1156
孤街浪徒
孤街浪徒 2021-01-30 13:57

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

I can export it using COPY command from postgres like -

COPY foo_table to \'/tmp/foo_table.csv

3条回答
  •  梦如初夏
    2021-01-30 14:40

    Expanding a bit on @Joey's answer, below adds support for a couple more features available in the manual.

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

    If you have capital letters in your table name (woe be onto you), you need the \" before and after the table name.

    The second thing I've added is column listing.

    Also note from the docs:

    This operation is not as efficient as the SQL COPY command because all data must pass through the client/server connection. For large amounts of data the SQL command might be preferable.

提交回复
热议问题