Re: How can I merge two tables?

From: Jeff Davis <pgsql(at)j-davis(dot)com>
To: Jerry LeVan <jerry(dot)levan(at)gmail(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org general" <pgsql-general(at)postgresql(dot)org>
Subject: Re: How can I merge two tables?
Date: 2011-09-03 02:08:08
Message-ID: 1315015688.8877.20.camel@jdavis-ux.asterdata.local
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, 2011-09-01 at 13:09 -0400, Jerry LeVan wrote:
> As time goes by the tables on the various computers get out of
> sync.
>
> Is there an elegant way I can get all of the differences (uniquely)
> merged into a single table?

You can try a query involving NOT EXISTS, combined with dblink:

http://www.postgresql.org/docs/current/static/dblink.html

Effectively the query would be something like:

INSERT INTO registrations
SELECT * FROM
-- fetch remote version of table
dblink(..., "SELECT * FROM registrations") AS remote_reg(...)
WHERE NOT EXISTS
(SELECT 1 FROM registrations local_reg
WHERE local_reg.id = remote_reg.id);

(disclaimer: I didn't test this query out, it's just for illustrating
the idea).

Regards,
Jeff Davis

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Michael A. Peters 2011-09-03 03:18:17 CentOS 6 - www.pgrpms.org - SELinux
Previous Message Rich Shepard 2011-09-03 00:18:52 Re: Looking for an intro-to-SQL book which is PostgreSQL-friendly