Re: Generating synthetic keys on copy

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Scott Ribe <scott_ribe(at)killerbytes(dot)com>
Cc: PostgreSQL General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Generating synthetic keys on copy
Date: 2006-10-05 00:15:24
Message-ID: 24773.1160007324@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Scott Ribe <scott_ribe(at)killerbytes(dot)com> writes:
> I have data I'm extracting from a legacy database. I want to assign
> newly-generated synthetic keys. I would like to use copy to get the data in.
> If I put an explicit null in the data file to be imported, pg won't generate
> a key, right?

Right. Instead, specify a column list to the COPY (you are using a PG
version new enough to have column lists in COPY, no?) and it will
execute the default expression for the column(s) not coming from the data
file.

If it is an old version, what I'd do is COPY into a temp table whose
column set matches the data file, and then use INSERT/SELECT to transfer
the data to the permanent table and fill the missing columns. This
latter is a good answer anytime you need to do extra data massaging
that COPY can't handle.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2006-10-05 01:50:56 Re: &nbsp;&nbsp;&nbsp;&nbsp;H
Previous Message stevegy 2006-10-04 23:43:03 Re: &nbsp;&nbsp;&nbsp;&nbsp;H