How to export table as CSV with headings on Postgresql? How to export table as CSV with headings on Postgresql? postgresql postgresql

How to export table as CSV with headings on Postgresql?


COPY products_273 TO '/tmp/products_199.csv' WITH (FORMAT CSV, HEADER);

as described in the manual.


From psql command line:

\COPY my_table TO 'filename' CSV HEADER

no semi-colon at the end.


instead of just table name, you can also write a query for getting only selected column data.

COPY (select id,name from tablename) TO 'filepath/aa.csv' DELIMITER ',' CSV HEADER;

with admin privilege

\COPY (select id,name from tablename) TO 'filepath/aa.csv' DELIMITER ',' CSV HEADER;