Skip to content

Latest commit

 

History

History
21 lines (13 loc) · 796 Bytes

export-from-a-table-or-query-to-csv.md

File metadata and controls

21 lines (13 loc) · 796 Bytes

Export From A Table Or Query To CSV

Category: Postgres

You can export data from a table or query to a CSV file using the COPY command. Data will be transferred from the Postgres host to the machine the command is being run on.

Copy all data from a table called employees:

COPY employees TO '/path/to/file/employees_data.csv';

Copy columns from a SELECT statement into a compressed file by piping the output through an external compression program:

COPY (SELECT first_name, last_name, start_date, email FROM employees) TO PROGRAM 'zip > /path/to/file/employees_data.zip';

You must have permission to write the output from the COPY command to the local filesystem.

See Postgres COPY for more details.