Re: selective copy

From: Jeff Eckermann <jeff_eckermann(at)yahoo(dot)com>
To: Matthew Nuzum <cobalt(at)bearfruit(dot)org>, pgsql-general(at)postgresql(dot)org
Subject: Re: selective copy
Date: 2003-02-27 15:27:29
Message-ID: 20030227152729.64973.qmail@web20802.mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

--- Matthew Nuzum <cobalt(at)bearfruit(dot)org> wrote:
> Hey group,
> I know you can't do a selective copy but I was
> wondering if someone can
> suggest the most effective way to get the same
> result.
>
> What I have is a lot of text data with unknown
> characters. Quotes,
> apostrophes, carriage returns and etc. There could
> be hundreds or thousands
> of rows in the table but I only want one or two. I
> need to get the data
> into an external file so it can be transported to
> another database and
> re-inserted.
>
> What I've done in the past is dump the table's data
> using pg_dump -t table
> and using INSERTS instead of copy and then grep the
> results so that I only
> get the desired line. That only works when I'm
> using simple data that
> doesn't wrap to different lines.
>
> I'm doing this to restore data from a backup :`(
>

Why not just a select?
>From psql:
\o filename -- to send the select output to a file
\a -- get rid of output space padding
\f '^V^I' -- type that to get tab separators
\t -- output tuples only
select * from tablename where (criteria for finding
the rows you want)
\c otherdatabase
\copy othertablename from filename with null as ''

If you need to handle special characters then you can
try enumerating your select list as:
select quote_literal(fieldname1),
quote_literal(fieldname2)...
for all of your text-type fields (warning: I have not
made much use of "quote_literal()" myself, so I may be
making undue assumptions about what it will do: please
test!).
If you want to emulate exactly the output of "copy",
then you could do:
select coalesce(fieldname,'\N') from ...

__________________________________________________
Do you Yahoo!?
Yahoo! Tax Center - forms, calculators, tips, more
http://taxes.yahoo.com/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2003-02-27 15:27:31 Re: pg_restore in debian postgresql 7.3.2-3
Previous Message Marc G. Fournier 2003-02-27 15:24:51 Re: PostgreSQL Replication Server licensing