Export PostgreSQL table to CSV [COPY command]


As PostgreSQL COPY command is convenient, I wrote this tips.
This command is easy and really useful, so please try it.

$ psql
-- Export product table to CSV file
\copy (SELECT * FROM products ORDER BY id) TO '/tmp/produccts.csv' DELIMITER ',' CSV HEADER
# If you want to see the csv including Shift-JIS(Japanese)
nkf -s /tmp/products.csv > /tmp/converted.csv

🍮 Special Thanks

🖥 Recommended VPS Service

VULTR provides high performance cloud compute environment for you. Vultr has 15 data-centers strategically placed around the globe, you can use a VPS with 512 MB memory for just $ 2.5 / month ($ 0.004 / hour). In addition, Vultr is up to 4 times faster than the competition, so please check it => Check Benchmark Results!!