Re: Exporting postgres query to CSV

From: John Purser <jmpurser(at)gmail(dot)com>
To: Ryan Suarez <ryan(dot)suarez(at)sheridanc(dot)on(dot)ca>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Exporting postgres query to CSV
Date: 2006-05-09 15:57:57
Message-ID: 20060509085757.cbd95387.jmpurser@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, 09 May 2006 10:58:07 -0400
Ryan Suarez <ryan(dot)suarez(at)sheridanc(dot)on(dot)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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Blair Lowe 2006-05-09 16:09:46 Re: pg_dump and grants to PUBLIC
Previous Message Joshua D. Drake 2006-05-09 15:51:39 Re: Arguments Pro/Contra Software Raid