Exporting postgres query to CSV

Поиск
Список
Период
Сортировка
Искать

Exporting postgres query to CSV

От:
Ryan Suarez <ryan.suarez@sheridanc.on.ca>
Дата:
Greetings,

I am running postgres 7.4.7 on debian sarge. 

I need to run an SQL query and store the results in a file.  The format 
needs to be comma separated values (CSV), so I can import this later in 
Excel.

Any ideas on how to accomplish this?

much appreciated,
Ryan

Re: Exporting postgres query to CSV

От:
John Purser <jmpurser@gmail.com>
Дата:
On Tue, 09 May 2006 10:58:07 -0400
Ryan Suarez  wrote:

> Greetings,
> 
> I am running postgres 7.4.7 on debian sarge. 
> 
> I need to run an SQL query and store the results in a file.  The
> format needs to be comma separated values (CSV), so I can import this
> later in Excel.
> 
> Any ideas on how to accomplish this?
> 
> much appreciated,
> Ryan
> 
> 
> ---------------------------(end of
> broadcast)--------------------------- TIP 6: explain analyze is your
> friend

Ryan,

Two tips.

First:
psql -U  -o  --pset format=unaligned
--pset fieldsep=',' -c '' -d 

I think that will give you the output you were after assuming you're
scripting psql and that you replace the values in <> with appropriate
values. The syntax is slightly different from the psql command line.

Second:
man psql is your friend.

John Purser

Re: Exporting postgres query to CSV

От:
SCassidy@overlandstorage.com
Дата:
A word of advice: if there is any chance that a column (e.g. text) contains
an embedded newline, you will be much better off outputting the data in
simple xml, instead of CSV.  This works very well with Excel for import.  I
just did a simple program for this recently.

Susan


                                                                                                                                          
                           Ryan Suarez                                                                                                    
                                               cc:                                                                                  
                           Sent by:                  Subject:  [GENERAL] Exporting postgres query to CSV                                  
                                                                                                                                          
                                                      |-------------------|                                                               
                      pgsql-general-owner@pos         | [ ] Expand Groups |                                                               
                      tgresql.org                     |-------------------|                                                               
                                                                                                                                          
                                                                                                                                          
                           05/09/2006 07:58                                                                                               
                      AM                                                                                                                  
                                                                                                                                          
                                                                                                                                          




Greetings,

I am running postgres 7.4.7 on debian sarge.

I need to run an SQL query and store the results in a file.  The format
needs to be comma separated values (CSV), so I can import this later in
Excel.

Any ideas on how to accomplish this?

much appreciated,
Ryan


---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend





----------------------------------------------------------------------------------------------
Simply protected storage solutions ensure that your information is
automatically safe, readily available and always there, visit us at http://www.overlandstorage.com
----------------------------------------------------------------------------------------------

Re: Exporting postgres query to CSV

От:
Nis Jorgensen <nis@superlativ.dk>
Дата:
SCassidy@overlandstorage.com wrote:
> A word of advice: if there is any chance that a column (e.g. text) contains
> an embedded newline, you will be much better off outputting the data in
> simple xml, instead of CSV.  This works very well with Excel for import.  I
> just did a simple program for this recently.

If the csv generator correctly quotes values containing quotes, commas
and newlines, Excel is going to be happy with the csv file as well

/Nis

Re: Exporting postgres query to CSV

От:
"Jim C. Nasby" <jnasby@pervasive.com>
Дата:
On Tue, May 09, 2006 at 08:57:57AM -0700, John Purser wrote:
> > I am running postgres 7.4.7 on debian sarge. 
> First:
> psql -U  -o  --pset format=unaligned
> --pset fieldsep=',' -c '' -d 
> 
> I think that will give you the output you were after assuming you're
> scripting psql and that you replace the values in <> with appropriate
> values. The syntax is slightly different from the psql command line.
> 
> Second:
> man psql is your friend.

Third:
7.4.7 is ancient and suffers from a number of data loss bugs. If you
need to stick with 7.4 you should at least be running the most current
version.
-- 
Jim C. Nasby, Sr. Engineering Consultant      jnasby@pervasive.com
Pervasive Software      http://pervasive.com    work: 512-231-6117
vcard: http://jim.nasby.net/pervasive.vcf       cell: 512-569-9461
FAQ