Exporting postgres query to CSV

Started by Ryan Suarezalmost 20 years ago5 messagesgeneral
Jump to latest
#1Ryan 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

#2John Purser
jmpurser@gmail.com
In reply to: Ryan Suarez (#1)
Re: Exporting postgres query to CSV

On Tue, 09 May 2006 10:58:07 -0400
Ryan Suarez <ryan.suarez@sheridanc.on.ca> 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 <PGSQL USER> -o <OUPUT FILE NAME> --pset format=unaligned
--pset fieldsep=',' -c '<SQL COMMAND HERE>' -d <DATABASE NAME HERE>

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

#3Noname
SCassidy@overlandstorage.com
In reply to: John Purser (#2)
Re: Exporting postgres query to CSV

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
<ryan.suarez@sheridanc. To: pgsql-general@postgresql.org
on.ca> 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
----------------------------------------------------------------------------------------------

#4Nis Jørgensen
nis@superlativ.dk
In reply to: Noname (#3)
Re: Exporting postgres query to CSV

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

#5Jim Nasby
Jim.Nasby@BlueTreble.com
In reply to: John Purser (#2)
Re: Exporting postgres query to CSV

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 <PGSQL USER> -o <OUPUT FILE NAME> --pset format=unaligned
--pset fieldsep=',' -c '<SQL COMMAND HERE>' -d <DATABASE NAME HERE>

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