Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create CSV file from SQL?

Tags:

postgresql

I am wondering how to create or export a CSV file from SQL? Is there any function for that similar to pgsql2shp? I would appreciate your ideas, tip or solutions.

like image 722
Z77 Avatar asked Apr 10 '26 20:04

Z77


2 Answers

You can save a complete table as a file using this command:

COPY tablename TO STDOUT CSV

Ref: https://www.postgresql.org/docs/current/static/sql-copy.html

like image 171
vartec Avatar answered Apr 13 '26 13:04

vartec


You can give this a try. But i believe there may be some syntax changes depending on the version.

COPY (SELECT foo,bar FROM whatever) TO ‘/tmp/dump.csv’ WITH CSV HEADER
like image 38
gruntled Avatar answered Apr 13 '26 15:04

gruntled



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!