Re: Exporting 1 text column from all rows from a table to a file per row?

From: Ondrej Ivanič <ondrej(dot)ivanic(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Exporting 1 text column from all rows from a table to a file per row?
Date: 2011-11-21 22:01:43
Message-ID: CAM6mieLVN0gDbmt1sZCoqPzwjOO_-3LfbhAe+3uJZg_+695+ew@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

On 22 November 2011 06:10, Joost Kraaijeveld <J(dot)Kraaijeveld(at)askesis(dot)nl> wrote:
> Is it possible, and if so how, to export a single column of a table into
> a separate file per row? I have a table with ~21000 rows that have a
> column "body1" containing ASCII text and I want to have 21000 separate
> ASCII files, each containing that column "body1". The name of the file
> does not matter, although it would be nice if they had the extension
> "txt".

Something like this could do the job:

for i in `psql -a -t -c 'select id from T'`; do psql -a -t -c "select
body1 from T where id = $i" -o $i.txt; done;

where 'T' is your table and 'id' is primary column in that table. I've
omitted connection options (-h -U ...) from psql commands.

--
Ondrej Ivanic
(ondrej(dot)ivanic(at)gmail(dot)com)

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Twaha Daudi 2011-11-21 22:06:15 Installing postgresql on windows 7
Previous Message Jerry Sievers 2011-11-21 21:51:23 Re: Exporting 1 text column from all rows from a table to a file per row?