Posted By

benpjohnson on 05/19/08


sql query csv postgres

Versions (?)

Postgres output CSV

 / Published in: Other


  1. \f ','
  2. \a
  3. \t
  4. \o /tmp/moocow.csv
  5. SELECT foo,bar FROM whatever;
  6. \o
  7. \q
  9. If a field has newlines, this will break. You can do something like this instead.....
  11. SELECT foo, bar, '"' || REPLACE(REPLACE(field_with_newilne, '\n', '\\n'), '"', '""') || '"' FROM whatever;

Report this snippet  


RSS Icon Subscribe to comments
Posted By: benpjohnson on June 5, 2008

After much pain:

COPY (SELECT foo,bar FROM whatever) TO '/tmp/dump.csv' WITH CSV HEADER

is cleaner and nicer and does escaping where needed for you

You need to login to post a comment.