Re: Export CSV from psql

From: Matthew <pgmail(at)homenurses(dot)com(dot)au>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Export CSV from psql
Date: 2003-11-23 22:36:53
Message-ID: 3FC13685.7000902@homenurses.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I have been playing with this lately, and I have found that if you use

\f ,
\o /tmp/output.csv

it opens fine in excel, it just bypasses the need to go
next, next, finish when opening from a TAB separated .txt file

Matt

Craig O'Shannessy wrote:
> Yeah, if you change the field separator to TAB, it will import cleanly
> into excel.
>
> Try this
>
> -- \f [STRING] show or set field separator for unaligned query output
> -- Note, to put in a TAB in psql, you will need to quote it, and put it
> -- in using Ctrl-V TAB (Ctrl-V tells the readline library to not interpret
> -- the next character I think, so you can use it to insert newlines and
> -- tabs etc)
>
> \f ' '
> \a
> \t
> \o outputfile.txt
> select .....
> \o
>
> This file should cleanly import into excel, excel even defaults to TAB
> when you open it, you just say open, then next,next,finish.
>
> Craig
>
> On Thu, 20 Nov 2003, Alex wrote:
>
>
>>you are right with the commas as delimiters , just sometimes you want to
>>export it directly as csv for import into excel.
>>
>>thanks , got it now with your hints.
>>alex
>>
>>Craig O'Shannessy wrote:
>>
>>
>>>I use the standard pipe signs as delimiters, comma's are a pretty silly
>>>delimiter if you ask me. Try putting this at the top of your psql query
>>>
>>>-- \a toggle between unaligned and aligned output mode
>>>-- \t show only rows (currently off)
>>>-- \o [FILE] send all query results to file or |pipe
>>>\a
>>>\t
>>>\o /tmp/outputfile.txt
>>>select ......
>>>\o
>>>
>>>Don't know if this helps you, or if you really need CSV. Someone probably
>>>has written a CSV export I 'spose.
>>>
>>>Best of luck.
>>>
>>>Craig
>>>
>>>
>>>On Thu, 20 Nov 2003, Alex wrote:
>>>
>>>
>>>
>>>
>>>>Hi,
>>>>is there a way to display the table in CSV format or write a query in
>>>>csv to a file ?
>>>>
>>>>Alex
>>>>
>>>>
>>>>
>>>>---------------------------(end of broadcast)---------------------------
>>>>TIP 2: you can get off all lists at once with the unregister command
>>>> (send "unregister YourEmailAddressHere" to majordomo(at)postgresql(dot)org)
>>>>
>>>>
>>>>
>>>
>>>
>>>
>>>
>>>
>>
>>
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://archives.postgresql.org
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Stephen Robert Norris 2003-11-23 23:39:48 7.4 Python configure hosed
Previous Message Alex Satrapa 2003-11-23 22:25:10 Re: Open source data modeling tools for PostgreSQL