dbTalk Databases Forums  

copy to CSV with header row?

comp.databases.postgresql comp.databases.postgresql


Discuss copy to CSV with header row? in the comp.databases.postgresql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
kinaole
 
Posts: n/a

Default copy to CSV with header row? - 12-13-2006 , 03:46 PM






I have looked and looked, and cannot find an answer to a simple
challenge.

I want to export a CSV file from postgres, but I would like to include
the collumn names as the top row of the file to make it easier for
folks who ope the file in 'a spreadsheet of their choice' to
undersatand the data.

The CSV export is pretty simple, if I am willing to add the headers by
hand:

psql foo -c "begin; create table blah as select * from my_summary
order by provider, period_end; copy blah to '/tmp/blah.csv' CSV;
rollback "


I cannot find a switch or other option that I can switch on to achieve
this. Any hints?

--
aloha (and thanks in advance),
dave


Reply With Quote
  #2  
Old   
HansH
 
Posts: n/a

Default Re: copy to CSV with header row? - 12-13-2006 , 06:57 PM






"kinaole" <kinaole (AT) gmail (DOT) com> schreef in bericht
news:1166046391.536602.137180 (AT) 73g2000cwn (DOT) googlegroups.com...
Quote:
I want to export a CSV file from postgres, but I would like to include
the collumn names as the top row of the file to make it easier for
folks who ope the file in 'a spreadsheet of their choice' to
undersatand the data.

The CSV export is pretty simple, if I am willing to add the headers by
hand:

psql foo -c "begin; create table blah as select * from my_summary
order by provider, period_end; copy blah to '/tmp/blah.csv' CSV;
rollback "


I cannot find a switch or other option that I can switch on to achieve
this. Any hints?
http://www.postgresql.org/docs/8.2/i.../sql-copy.html
COPY { tablename [ ( column [, ...] ) ] | ( query ) }
TO { 'filename' | STDOUT }
[ [ WITH ]
[ BINARY ]
[ HEADER ]
[ OIDS ]
[ DELIMITER [ AS ] 'delimiter' ]
[ NULL [ AS ] 'null string' ]
[ CSV [ HEADER ]
[ QUOTE [ AS ] 'quote' ]
[ ESCAPE [ AS ] 'escape' ]
[ FORCE QUOTE column [, ...] ]

So I'ld say for version 8.1 and above without testing
copy ( select * from my_summary
order by provider, period_end )
to '/tmp/blah.csv' with csv header


HansH




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.