Обсуждение: dumping query results to a csv

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

dumping query results to a csv

От
David Durham
Дата:
This is kind of a pg-admin newbie question, so apologies in advance.

Anyway, I'd like to issue a command that dumps the results of a query to
a txt file in comma delimited format.  Does PostgreSQL ship with
something to do this?  I searched the web, but found what appeared to be
non-free solutions.

Thanks,

Dave


Re: dumping query results to a csv

От
Jeff Frost
Дата:
David,

You're probably looking for something like this from the psql man page:

        -F separator

        --field-separator separator
               Use  separator  as  the  field  separator. This is equivalent to
               \pset fieldsep or \f.

I would guess -F "," would do the trick.

On Thu, 25 Aug 2005, David Durham wrote:

> This is kind of a pg-admin newbie question, so apologies in advance.
>
> Anyway, I'd like to issue a command that dumps the results of a query to
> a txt file in comma delimited format.  Does PostgreSQL ship with
> something to do this?  I searched the web, but found what appeared to be
> non-free solutions.
>
> Thanks,
>
> Dave
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: don't forget to increase your free space map settings
>

--
Jeff Frost, Owner     <jeff@frostconsultingllc.com>
Frost Consulting, LLC     http://www.frostconsultingllc.com/
Phone: 650-780-7908    FAX: 650-649-1954

Re: dumping query results to a csv

От
Adi Alurkar
Дата:
Greetings,

There is no direct way to achieve what you want, the easiest hack is
to create a temp table with you query i.e.

create table tmp_foo as select col1, col4, col7 from table1, table2
where  ....;
copy table tmp_foo to [stdout|<file_name>]

HTH

Adi Alurkar
adi@sf.net


On Aug 25, 2005, at 3:24 PM, David Durham wrote:

> This is kind of a pg-admin newbie question, so apologies in advance.
>
> Anyway, I'd like to issue a command that dumps the results of a
> query to
> a txt file in comma delimited format.  Does PostgreSQL ship with
> something to do this?  I searched the web, but found what appeared
> to be
> non-free solutions.
>
> Thanks,
>
> Dave
>
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 5: don't forget to increase your free space map settings
>


Re: dumping query results to a csv

От
Jeff Frost
Дата:
Whoops, should have also mentioned that you want -P format=unaligned like so:

psql -P format=unaligned -F ',' snort <<EOF
select * from iphdr;
EOF

On Thu, 25 Aug 2005, Jeff Frost wrote:

> David,
>
> You're probably looking for something like this from the psql man page:
>
>       -F separator
>
>       --field-separator separator
>              Use  separator  as  the  field  separator. This is equivalent
> to
>              \pset fieldsep or \f.
>
> I would guess -F "," would do the trick.
>
> On Thu, 25 Aug 2005, David Durham wrote:
>
>> This is kind of a pg-admin newbie question, so apologies in advance.
>>
>> Anyway, I'd like to issue a command that dumps the results of a query to
>> a txt file in comma delimited format.  Does PostgreSQL ship with
>> something to do this?  I searched the web, but found what appeared to be
>> non-free solutions.
>>
>> Thanks,
>>
>> Dave
>>
>>
>> ---------------------------(end of broadcast)---------------------------
>> TIP 5: don't forget to increase your free space map settings
>>
>
>

--
Jeff Frost, Owner     <jeff@frostconsultingllc.com>
Frost Consulting, LLC     http://www.frostconsultingllc.com/
Phone: 650-780-7908    FAX: 650-649-1954

Re: dumping query results to a csv

От
"Jim C. Nasby"
Дата:
On Thu, Aug 25, 2005 at 03:48:54PM -0700, Adi Alurkar wrote:
> Greetings,
>
> There is no direct way to achieve what you want, the easiest hack is
> to create a temp table with you query i.e.
>
> create table tmp_foo as select col1, col4, col7 from table1, table2
> where  ....;
> copy table tmp_foo to [stdout|<file_name>]

Can we make this a TODO? It would certainly be handy to be able to COPY
directly from a query.

In the mean time, you can also do something like

SELECT field1 || ',' || field2 || ',' || field3

and capture the output of that to a file, but that's an uglier hack than
the temptable trick.
--
Jim C. Nasby, Sr. Engineering Consultant      jnasby@pervasive.com
Pervasive Software        http://pervasive.com        512-569-9461

Re: dumping query results to a csv

От
Steve Crawford
Дата:
On Thursday 25 August 2005 3:24 pm, David Durham wrote:
> This is kind of a pg-admin newbie question, so apologies in
> advance.
>
> Anyway, I'd like to issue a command that dumps the results of a
> query to a txt file in comma delimited format.  Does PostgreSQL
> ship with something to do this?  I searched the web, but found what
> appeared to be non-free solutions.

Use heredoc notation to set the format to unaligned, set your field
separator (and, if necessary, record separator) to whatever you want,
turn off the footer, and run the output to a file:

psql <any needed connection parameters> --quiet databasename <<EOT
\pset format unaligned
\pset fieldsep ','
\pset footer
\o youroutputfile.csv
select ......
EOT

Or if you prefer everything on the command line:
psql <any needed connection parameters> --quiet --no-align
--field-separator ',' --pset footer --output youroutputfile.csv
--command <select .......> databasename

Optionally add \pset tuples-only (first example) or --tuples-only
(second example) if you do not want the header line with field names
to be included. Note, if you use tuples only, you don't need to turn
off the footer separately. You can also use the short versions of all
the command line switches if you prefer. "man psql"

Cheers,
Steve


Re: dumping query results to a csv

От
David Durham
Дата:
Adi Alurkar wrote:
> Greetings,
>
> There is no direct way to achieve what you want, the easiest hack is  to
> create a temp table with you query i.e.
>
> create table tmp_foo as select col1, col4, col7 from table1, table2
> where  ....;
> copy table tmp_foo to [stdout|<file_name>]
>
> HTH

I ended up using pgadmin3 -- which has a nice feature for writing the
contents of a query to file.

Of course, I'm still going to follow this subject and look at other more
scriptable ways of doing this.

Thanks,

Dave