Re: Moving Specific Data Across Schemas Including FKs - Mailing list pgsql-general

From Raymond O'Donnell
Subject Re: Moving Specific Data Across Schemas Including FKs
Date
Msg-id 55393624.8070105@iol.ie
Whole thread Raw
In response to Re: Moving Specific Data Across Schemas Including FKs  (Raymond O'Donnell <rod@iol.ie>)
List pgsql-general
On 23/04/2015 19:08, Raymond O'Donnell wrote:
> On 23/04/2015 18:09, Cory Tucker wrote:
>> I have the need to move a specific set of data from one schema to
>> another.  These schemas are on the same database instance and have all
>> of the same relations defined.  The SQL to copy data from one table is
>> relatively straightforward:
>>
>> INSERT INTO schema_b.my_table
>> SELECT * FROM schema_a.my_table WHERE ...
>>
>> What I am trying to figure out is that if I also have other relations
>> that have foreign keys into the data I am moving, how would I also move
>> the data from those relations and maintain the FK integrity?
>
> I'd create the tables in the new schema without the FK constraints, copy
> the data, then add the constraints afterwards.

Meant to add, you'll also need to do

  select setval(...);

on the sequence(s) in the new schema supplying the ID values, to set
them to something higher than any extant values copied in from the old
schema... but I'm sure you thought of that. :-)

Ray.

--
Raymond O'Donnell :: Galway :: Ireland
rod@iol.ie


pgsql-general by date:

Previous
From: Raymond O'Donnell
Date:
Subject: Re: Moving Specific Data Across Schemas Including FKs
Next
From: billythebomber
Date:
Subject: BDR Across Distributed Nodes