We Recommend

Beginning XML Beginning XML
The perfect resource for beginning XML programmers, this guidebook shows you what XML is, how to use it, and what technologies surround it. The authors build on the strengths of previous editions while covering the latest changes in the XML landscape such as XQuery, RSS and Atom, and Ajax.


Posted By

benpjohnson on 05/19/08


Tagged

sql query csv postgres


Versions (?)


Postgres output CSV


Published in: Other 


URL: http://pookey.co.uk/blog/archives/49-Outputting-from-Postgres-to-CSV.html

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

Report this snippet 

Comments

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.