Re: Synchronizing a table that is in two different databases : Need to dump a table as inserts from db1 and change the insert statements into UPDATE statements

Started by Khangelani Gamaabout 12 years ago10 messagesgeneral
Jump to latest
#1Khangelani Gama
kgama@argility.com

Hi Chris or anyone who can help

When I try this just below, it complains about foo , saying "ERROR: schema
"foo" does not exist." I got stuck on this error for a while now but still
trying to see why but still no luck so far. If you have something please
help

begin;

update foo

set br_desc = bar.br_desc

, br_active = bar.br_active

(rest of columns)

where foo.br_cde = bar.br_cde;

databases has got:

SHOW search_path;

search_path

----------------

"$user",public

(1 row)

*From:* Khangelani Gama [mailto:kgama@argility.com]
*Sent:* Friday, March 28, 2014 7:52 AM
*To:* 'chris@chriscurvey.com'; 'pgsql'
*Subject:* RE: [GENERAL] Synchronizing a table that is two different
databases : Need to dump a table a insert from db1 and change the insert
statements into UPDATE statements

Thank you very much, I will try option 2 because option 1 will give me FK
constrains. These two databases are not exactly similar, but few tables on
each of these Databases needs to be exactly the same, in this case db1 is
the reliable one, hence that's why I need to update db2 with what it's in
db1.

*From:* ccurvey@gmail.com [mailto:ccurvey@gmail.com <ccurvey@gmail.com>] *On
Behalf Of *Chris Curvey
*Sent:* Thursday, March 27, 2014 3:08 PM
*To:* Khangelani Gama; pgsql
*Subject:* Re: [GENERAL] Synchronizing a table that is two different
databases : Need to dump a table a insert from db1 and change the insert
statements into UPDATE statements

On Thu, Mar 27, 2014 at 6:16 AM, Khangelani Gama <kgama@argility.com> wrote:

Hi all

Synchronizing a *table* that is in two different databases(e.g *db1 and db2*).

Please help me with this. I need to dump a table as INSERTS from db1
(postgres 9.1.2) and change the INSERT statements into UPDATE statements in
order to apply that change in *db2(postgres 8.3.0.112)* which has the same
*table* as *db1*. Where the record does not exist I need to insert that
record. There is more than 1000 INSERTS I need to convert to UPDATES for
the same table. Please help .

*Example:*

Below it's the INSERT from postgres 9.1.2 DATABASE which should convert to
UPDATE statement.

INSERT INTO* table* (br_cde, br_desc, br_active, br_closed, grp_cde,
ctry_cde, state_cde, br_addr_line1, br_addr_line2, br_addr_line3,
br_addr_line4, br_addr_cde, br_post_addr_line1, br_post_addr_line2,
br_post_addr_line3, br_post_addr_line4, br_post_addr_cde, br_phone,
tax_cde, br_server_name, br_lang_ind, bureau_cde, br_risk_typ_cde,
br_access_ccd, br_access_bureau, br_network_active, br_desc_short,
br_is_trading, br_is_test, br_is_nomodem, br_is_whs, br_is_merch,
br_is_cco, br_is_central, br_is_merch_active, br_central_brn, br_merch_brn,
br_cco_brn, br_cgo_brn, br_cluster_brn, br_contact, br_tollfree, br_fax,
br_telex, br_email, ops_cde, ops_director, br_cmpy_reg, br_tax_reg,
audit_id, comp_cde, br_batch, br_release_pilot_type, br_is_order_active,
bnk_id, br_class_cde, br_erp_xref, br_to_open, br_to_close, br_comments,
br_is_hire, br_is_voucher, br_is_clear_center, br_is_headoffice,
br_is_nsp_active, usr_pass_history, br_network_protocol,
br_gen_ins_central_brn, br_cpi_ins_central_brn, br_rsc_area, br_rsc_perc,
br_switch_deduc, br_tvlic_dealer_no, br_cco_followup, br_triad_live,
br_do_cust_locking, br_is_rainbow, br_nca_live, br_ncr_reg_no, prov_cde,
br_is_data_archive, br_icu_brn, br_living_expenses_live, br_ab_dorder_live,
br_ab_receipts_active, br_spiv_max_alloc_perc, br_merger_brn,
br_dep_bnk_acc, br_merchant_number, br_goods_ins_live, br_cgs_connection,
whs_cde, br_crm_brn, subscription_active, br_prn_doc_default_active,
br_use_jms, br_sso_active, br_paym8_properties, creditors_supp_no_prefix,
br_block_debtors_obo, ccms_branch, br_is_ccms_live, ceres_conv_flag,
cims_branch, br_is_cims_live, br_accept_sql_releases) VALUES ('9940',
'ELLERINES CENTRAL - 9940', true, NULL, '1', 'BAF', 'BAF', '209 SMIT
STREET', 'BRA', 'Jgf', 'Jrg', '3025', '209 SMIT STREET', 'BRA', 'Jrg',
NULL, '2017', '(4562) 712 1300', '1', 'dfgike.com', 'ENG', 'ITC', 'L',
false, false, false, 'BATCH - 9940', false, false, false, false, false,
false, true, false, '9940', NULL, NULL, '10.52.1.31', '9940', 'DOUG', NULL,
'(4562) 712 1300' ', NULL, NULL, '001', NULL, '1969/02687/07', NULL, 0,
NULL, '9940', NULL, false, NULL, NULL, NULL, NULL, NULL, NULL, false,
false, false, false, false, 30, 'WS2~WS2', '1002', '1002', NULL, NULL,
false, NULL, NULL, false, true, false, true, 'NC', NULL, true, NULL, true,
true, NULL, NULL, NULL, NULL, NULL, true, 'oracle_live', NULL, NULL, true,
NULL, true, false, NULL, NULL, NULL, NULL, NULL, NULL, NULL, false, false);

(For clarity, I'm going to call the table in question "foo".)

Option 1: If you just want to do a full copy of db1.foo to db2.foo, then
do that. Dump foo from db1, truncate foo in db2 and load db2.foo from the
dump. But you might not be able to do that (foreign keys and such). In
that case..

Option 2: Dump the table from db1, then load it into a table with a
different name ("bar") in db2. Then you can do this:

begin;

update foo

set br_desc = bar.br_desc

, br_active = bar.br_active

(rest of columns)

where foo.br_cde = bar.br_cde;

insert into foo (br_cde, br_desc, br_active, ...)

select br_cde, br_desc, br_active, ....

from bar

where not exists

( select *

from foo

where foo.br_cde = bar.br_cde);

commit;

CONFIDENTIALITY NOTICE
The contents of and attachments to this e-mail are intended for the addressee only, and may contain the confidential
information of Argility (Proprietary) Limited and/or its subsidiaries. Any review, use or dissemination thereof by anyone
other than the intended addressee is prohibited.If you are not the intended addressee please notify the writer immediately
and destroy the e-mail. Argility (Proprietary) Limited and its subsidiaries distance themselves from and accept no liability
for unauthorised use of their e-mail facilities or e-mails sent other than strictly for business purposes.

#2Khangelani Gama
kgama@argility.com
In reply to: Khangelani Gama (#1)

Hi Chris or anyone who can help

When I try this just below, it complains about foo , saying "ERROR: schema
"foo" does not exist." I got stuck on this error for a while now but still
trying to see why but still no luck so far. If you have something please
help

begin;

update foo

set br_desc = bar.br_desc

, br_active = bar.br_active

(rest of columns)

where foo.br_cde = bar.br_cde;

databases has got:

SHOW search_path;

search_path

----------------

"$user",public

(1 row)

*From:* Khangelani Gama [mailto:kgama@argility.com <kgama@argility.com>]
*Sent:* Friday, March 28, 2014 7:52 AM
*To:* 'chris@chriscurvey.com'; 'pgsql'
*Subject:* RE: [GENERAL] Synchronizing a table that is two different
databases : Need to dump a table a insert from db1 and change the insert
statements into UPDATE statements

Thank you very much, I will try option 2 because option 1 will give me FK
constrains. These two databases are not exactly similar, but few tables on
each of these Databases needs to be exactly the same, in this case db1 is
the reliable one, hence that's why I need to update db2 with what it's in
db1.

*From:* ccurvey@gmail.com [mailto:ccurvey@gmail.com <ccurvey@gmail.com>] *On
Behalf Of *Chris Curvey
*Sent:* Thursday, March 27, 2014 3:08 PM
*To:* Khangelani Gama; pgsql
*Subject:* Re: [GENERAL] Synchronizing a table that is two different
databases : Need to dump a table a insert from db1 and change the insert
statements into UPDATE statements

On Thu, Mar 27, 2014 at 6:16 AM, Khangelani Gama <kgama@argility.com> wrote:

Hi all

Synchronizing a *table* that is in two different databases(e.g *db1 and db2*).

Please help me with this. I need to dump a table as INSERTS from db1
(postgres 9.1.2) and change the INSERT statements into UPDATE statements in
order to apply that change in *db2(postgres 8.3.0.112)* which has the same
*table* as *db1*. Where the record does not exist I need to insert that
record. There is more than 1000 INSERTS I need to convert to UPDATES for
the same table. Please help .

*Example:*

Below it's the INSERT from postgres 9.1.2 DATABASE which should convert to
UPDATE statement.

INSERT INTO* table* (br_cde, br_desc, br_active, br_closed, grp_cde,
ctry_cde, state_cde, br_addr_line1, br_addr_line2, br_addr_line3,
br_addr_line4, br_addr_cde, br_post_addr_line1, br_post_addr_line2,
br_post_addr_line3, br_post_addr_line4, br_post_addr_cde, br_phone,
tax_cde, br_server_name, br_lang_ind, bureau_cde, br_risk_typ_cde,
br_access_ccd, br_access_bureau, br_network_active, br_desc_short,
br_is_trading, br_is_test, br_is_nomodem, br_is_whs, br_is_merch,
br_is_cco, br_is_central, br_is_merch_active, br_central_brn, br_merch_brn,
br_cco_brn, br_cgo_brn, br_cluster_brn, br_contact, br_tollfree, br_fax,
br_telex, br_email, ops_cde, ops_director, br_cmpy_reg, br_tax_reg,
audit_id, comp_cde, br_batch, br_release_pilot_type, br_is_order_active,
bnk_id, br_class_cde, br_erp_xref, br_to_open, br_to_close, br_comments,
br_is_hire, br_is_voucher, br_is_clear_center, br_is_headoffice,
br_is_nsp_active, usr_pass_history, br_network_protocol,
br_gen_ins_central_brn, br_cpi_ins_central_brn, br_rsc_area, br_rsc_perc,
br_switch_deduc, br_tvlic_dealer_no, br_cco_followup, br_triad_live,
br_do_cust_locking, br_is_rainbow, br_nca_live, br_ncr_reg_no, prov_cde,
br_is_data_archive, br_icu_brn, br_living_expenses_live, br_ab_dorder_live,
br_ab_receipts_active, br_spiv_max_alloc_perc, br_merger_brn,
br_dep_bnk_acc, br_merchant_number, br_goods_ins_live, br_cgs_connection,
whs_cde, br_crm_brn, subscription_active, br_prn_doc_default_active,
br_use_jms, br_sso_active, br_paym8_properties, creditors_supp_no_prefix,
br_block_debtors_obo, ccms_branch, br_is_ccms_live, ceres_conv_flag,
cims_branch, br_is_cims_live, br_accept_sql_releases) VALUES ('9940',
'ELLERINES CENTRAL - 9940', true, NULL, '1', 'BAF', 'BAF', '209 SMIT
STREET', 'BRA', 'Jgf', 'Jrg', '3025', '209 SMIT STREET', 'BRA', 'Jrg',
NULL, '2017', '(4562) 712 1300', '1', 'dfgike.com', 'ENG', 'ITC', 'L',
false, false, false, 'BATCH - 9940', false, false, false, false, false,
false, true, false, '9940', NULL, NULL, '10.52.1.31', '9940', 'DOUG', NULL,
'(4562) 712 1300' ', NULL, NULL, '001', NULL, '1969/02687/07', NULL, 0,
NULL, '9940', NULL, false, NULL, NULL, NULL, NULL, NULL, NULL, false,
false, false, false, false, 30, 'WS2~WS2', '1002', '1002', NULL, NULL,
false, NULL, NULL, false, true, false, true, 'NC', NULL, true, NULL, true,
true, NULL, NULL, NULL, NULL, NULL, true, 'oracle_live', NULL, NULL, true,
NULL, true, false, NULL, NULL, NULL, NULL, NULL, NULL, NULL, false, false);

(For clarity, I'm going to call the table in question "foo".)

Option 1: If you just want to do a full copy of db1.foo to db2.foo, then
do that. Dump foo from db1, truncate foo in db2 and load db2.foo from the
dump. But you might not be able to do that (foreign keys and such). In
that case..

Option 2: Dump the table from db1, then load it into a table with a
different name ("bar") in db2. Then you can do this:

begin;

update foo

set br_desc = bar.br_desc

, br_active = bar.br_active

(rest of columns)

where foo.br_cde = bar.br_cde;

insert into foo (br_cde, br_desc, br_active, ...)

select br_cde, br_desc, br_active, ....

from bar

where not exists

( select *

from foo

where foo.br_cde = bar.br_cde);

commit;

CONFIDENTIALITY NOTICE
The contents of and attachments to this e-mail are intended for the addressee only, and may contain the confidential
information of Argility (Proprietary) Limited and/or its subsidiaries. Any review, use or dissemination thereof by anyone
other than the intended addressee is prohibited.If you are not the intended addressee please notify the writer immediately
and destroy the e-mail. Argility (Proprietary) Limited and its subsidiaries distance themselves from and accept no liability
for unauthorised use of their e-mail facilities or e-mails sent other than strictly for business purposes.

#3Khangelani Gama
kgama@argility.com
In reply to: Khangelani Gama (#1)

Hi Chris or anyone who can help

When I try this just below, it complains about foo (new table created in
db2) , saying *ERROR: schema foo does not exist*. I got stuck on this error
for a while now but still trying to see why but still no luck so far. If
you have something please help

begin;

update foo

set br_desc = bar.br_desc

, br_active = bar.br_active

(rest of columns)

where foo.br_cde = bar.br_cde;

*From:* Khangelani Gama [mailto:kgama@argility.com <kgama@argility.com>]
*Sent:* Friday, March 28, 2014 7:52 AM
*To:* 'chris@chriscurvey.com'; 'pgsql'
*Subject:* RE: [GENERAL] Synchronizing a table that is two different
databases : Need to dump a table a insert from db1 and change the insert
statements into UPDATE statements

Thank you very much, I will try option 2 because option 1 will give me FK
constrains. These two databases are not exactly similar, but few tables on
each of these Databases needs to be exactly the same, in this case db1 is
the reliable one, hence that's why I need to update db2 with what it's in
db1.

*From:* ccurvey@gmail.com [mailto:ccurvey@gmail.com <ccurvey@gmail.com>] *On
Behalf Of *Chris Curvey
*Sent:* Thursday, March 27, 2014 3:08 PM
*To:* Khangelani Gama; pgsql
*Subject:* Re: [GENERAL] Synchronizing a table that is two different
databases : Need to dump a table a insert from db1 and change the insert
statements into UPDATE statement

(For clarity, I'm going to call the table in question "foo".)

Option 1: If you just want to do a full copy of db1.foo to db2.foo, then
do that. Dump foo from db1, truncate foo in db2 and load db2.foo from the
dump. But you might not be able to do that (foreign keys and such). In
that case..

Option 2: Dump the table from db1, then load it into a table with a
different name ("bar") in db2. Then you can do this:

begin;

update foo

set br_desc = bar.br_desc

, br_active = bar.br_active

(rest of columns)

where foo.br_cde = bar.br_cde;

insert into foo (br_cde, br_desc, br_active, ...)

select br_cde, br_desc, br_active, ....

from bar

where not exists

( select *

from foo

where foo.br_cde = bar.br_cde);

commit;

CONFIDENTIALITY NOTICE
The contents of and attachments to this e-mail are intended for the addressee only, and may contain the confidential
information of Argility (Proprietary) Limited and/or its subsidiaries. Any review, use or dissemination thereof by anyone
other than the intended addressee is prohibited.If you are not the intended addressee please notify the writer immediately
and destroy the e-mail. Argility (Proprietary) Limited and its subsidiaries distance themselves from and accept no liability
for unauthorised use of their e-mail facilities or e-mails sent other than strictly for business purposes.

#4Alban Hertroys
haramrae@gmail.com
In reply to: Khangelani Gama (#3)

On 28 March 2014 11:07, Khangelani Gama <kgama@argility.com> wrote:

Hi Chris or anyone who can help

When I try this just below, it complains about foo (new table created in
db2) , saying ERROR: schema foo does not exist. I got stuck on this error
for a while now but still trying to see why but still no luck so far. If you
have something please help

begin;

update foo
set br_desc = bar.br_desc
, br_active = bar.br_active
(rest of columns)
where foo.br_cde = bar.br_cde;

Really? I would have expected ERROR: table or schema bar does not exist.

Look up the syntax for UPDATE FROM.

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#5Chris Curvey
chris@chriscurvey.com
In reply to: Khangelani Gama (#1)

On Fri, Mar 28, 2014 at 5:56 AM, Khangelani Gama <kgama@argility.com> wrote:

Hi Chris or anyone who can help

When I try this just below, it complains about foo , saying "ERROR:
schema "foo" does not exist." I got stuck on this error for a while now
but still trying to see why but still no luck so far. If you have something
please help

begin;

update foo

set br_desc = bar.br_desc

, br_active = bar.br_active

(rest of columns)

where foo.br_cde = bar.br_cde;

you need to replace "foo" and "bar" with the names of your tables.

#6Khangelani Gama
kgama@argility.com
In reply to: Chris Curvey (#5)

Hi Chris

I did replace foo and bar tables with my tables, in db2 I have table
foo(the one to be updated) and the table bar(the one which has data I got
from db1)

*From:* ccurvey@gmail.com [mailto:ccurvey@gmail.com] *On Behalf Of *Chris
Curvey
*Sent:* Friday, March 28, 2014 1:50 PM
*To:* Khangelani Gama
*Cc:* pgsql
*Subject:* Re: [GENERAL] Synchronizing a table that is in two different
databases : Need to dump a table as inserts from db1 and change the insert
statements into UPDATE statements

On Fri, Mar 28, 2014 at 5:56 AM, Khangelani Gama <kgama@argility.com> wrote:

Hi Chris or anyone who can help

When I try this just below, it complains about foo , saying "ERROR: schema
"foo" does not exist." I got stuck on this error for a while now but still
trying to see why but still no luck so far. If you have something please
help

begin;

update foo

set br_desc = bar.br_desc

, br_active = bar.br_active

(rest of columns)

where foo.br_cde = bar.br_cde;

you need to replace "foo" and "bar" with the names of your tables.

CONFIDENTIALITY NOTICE
The contents of and attachments to this e-mail are intended for the addressee only, and may contain the confidential
information of Argility (Proprietary) Limited and/or its subsidiaries. Any review, use or dissemination thereof by anyone
other than the intended addressee is prohibited.If you are not the intended addressee please notify the writer immediately
and destroy the e-mail. Argility (Proprietary) Limited and its subsidiaries distance themselves from and accept no liability
for unauthorised use of their e-mail facilities or e-mails sent other than strictly for business purposes.

#7Rémi Cura
remi.cura@gmail.com
In reply to: Khangelani Gama (#6)

Not sure, but maybe

update foo

set br_desc = bar.br_desc

, br_active = bar.br_active

(rest of columns)

where br_cde = bar.br_cde;

Anyway it seem sto be terribly bad idea to give those kind of names !

Cheers,

Rémi-C

#8Chris Curvey
chris@chriscurvey.com
In reply to: Khangelani Gama (#6)

I think the convention on this list is to bottom-post.

On Fri, Mar 28, 2014 at 7:59 AM, Khangelani Gama <kgama@argility.com> wrote:

Hi Chris

I did replace foo and bar tables with my tables, in db2 I have table
foo(the one to be updated) and the table bar(the one which has data I got
from db1)

*From:* ccurvey@gmail.com [mailto:ccurvey@gmail.com] *On Behalf Of *Chris
Curvey
*Sent:* Friday, March 28, 2014 1:50 PM
*To:* Khangelani Gama
*Cc:* pgsql
*Subject:* Re: [GENERAL] Synchronizing a table that is in two different
databases : Need to dump a table as inserts from db1 and change the insert
statements into UPDATE statements

On Fri, Mar 28, 2014 at 5:56 AM, Khangelani Gama <kgama@argility.com>
wrote:

Hi Chris or anyone who can help

When I try this just below, it complains about foo , saying "ERROR:
schema "foo" does not exist." I got stuck on this error for a while now
but still trying to see why but still no luck so far. If you have something
please help

begin;

update foo

set br_desc = bar.br_desc

, br_active = bar.br_active

(rest of columns)

where foo.br_cde = bar.br_cde;

you need to replace "foo" and "bar" with the names of your tables.

I'm stumped. Here's the test that I just ran on both 9.2 and 8.4, and I'm
not getting an error:

drop table if exists foo;
drop table if exists bar;

create table foo (a int, b int);
create table bar (a int, b int);

insert into foo values (1, 1);
insert into foo values (2, 2);

insert into bar values (1, 3);
insert into bar values (4, 4);

update foo
set b = bar.b
from bar
where foo.a = bar.a;

select * from bar;

Perhaps if you want to privately send me the exact statement you are using,
I can take quick look and see if there's something obvious.

#9Khangelani Gama
kgama@argility.com
In reply to: Chris Curvey (#8)

Thank you very much, I on my previous try I did not include the FROM. I
will just do thoroughly checks before COMMIT. Thanks very much.

*From:* ccurvey@gmail.com [mailto:ccurvey@gmail.com] *On Behalf Of *Chris
Curvey
*Sent:* Friday, March 28, 2014 2:38 PM
*To:* Khangelani Gama
*Cc:* pgsql
*Subject:* Re: [GENERAL] Synchronizing a table that is in two different
databases : Need to dump a table as inserts from db1 and change the insert
statements into UPDATE statements

I think the convention on this list is to bottom-post.

On Fri, Mar 28, 2014 at 7:59 AM, Khangelani Gama <kgama@argility.com> wrote:

Hi Chris

I did replace foo and bar tables with my tables, in db2 I have table
foo(the one to be updated) and the table bar(the one which has data I got
from db1)

*From:* ccurvey@gmail.com [mailto:ccurvey@gmail.com] *On Behalf Of *Chris
Curvey
*Sent:* Friday, March 28, 2014 1:50 PM
*To:* Khangelani Gama
*Cc:* pgsql
*Subject:* Re: [GENERAL] Synchronizing a table that is in two different
databases : Need to dump a table as inserts from db1 and change the insert
statements into UPDATE statements

On Fri, Mar 28, 2014 at 5:56 AM, Khangelani Gama <kgama@argility.com> wrote:

Hi Chris or anyone who can help

When I try this just below, it complains about foo , saying "ERROR: schema
"foo" does not exist." I got stuck on this error for a while now but still
trying to see why but still no luck so far. If you have something please
help

begin;

update foo

set br_desc = bar.br_desc

, br_active = bar.br_active

(rest of columns)

where foo.br_cde = bar.br_cde;

you need to replace "foo" and "bar" with the names of your tables.

I'm stumped. Here's the test that I just ran on both 9.2 and 8.4, and I'm
not getting an error:

drop table if exists foo;

drop table if exists bar;

create table foo (a int, b int);

create table bar (a int, b int);

insert into foo values (1, 1);

insert into foo values (2, 2);

insert into bar values (1, 3);

insert into bar values (4, 4);

update foo

set b = bar.b

from bar

where foo.a = bar.a;

select * from bar;

Perhaps if you want to privately send me the exact statement you are using,
I can take quick look and see if there's something obvious.

CONFIDENTIALITY NOTICE
The contents of and attachments to this e-mail are intended for the addressee only, and may contain the confidential
information of Argility (Proprietary) Limited and/or its subsidiaries. Any review, use or dissemination thereof by anyone
other than the intended addressee is prohibited.If you are not the intended addressee please notify the writer immediately
and destroy the e-mail. Argility (Proprietary) Limited and its subsidiaries distance themselves from and accept no liability
for unauthorised use of their e-mail facilities or e-mails sent other than strictly for business purposes.

#10Markella Skempri
markella_skembri@hotmail.com
In reply to: Khangelani Gama (#2)

have you tried

update foo

set br_desc = bar.br_desc

, br_active = bar.br_active

(rest of columns)

from bar

where foo.br_cde = bar.br_cde;

From: Khangelani Gama
Sent: Friday, March 28, 2014 9:59 AM
To: pgsql-general@postgresql.org
Subject: Re: [GENERAL] Synchronizing a table that is in two different databases : Need to dump a table as inserts from db1 and change the insert statements into UPDATE statements

Hi Chris or anyone who can help

When I try this just below, it complains about foo , saying “ERROR: schema “foo” does not exist.” I got stuck on this error for a while now but still trying to see why but still no luck so far. If you have something please help

begin;

update foo

set br_desc = bar.br_desc

, br_active = bar.br_active

(rest of columns)

where foo.br_cde = bar.br_cde;

databases has got:

SHOW search_path;

search_path

----------------

"$user",public

(1 row)

From: Khangelani Gama [mailto:kgama@argility.com]
Sent: Friday, March 28, 2014 7:52 AM
To: 'chris@chriscurvey.com'; 'pgsql'
Subject: RE: [GENERAL] Synchronizing a table that is two different databases : Need to dump a table a insert from db1 and change the insert statements into UPDATE statements

Thank you very much, I will try option 2 because option 1 will give me FK constrains. These two databases are not exactly similar, but few tables on each of these Databases needs to be exactly the same, in this case db1 is the reliable one, hence that’s why I need to update db2 with what it’s in db1.

From: ccurvey@gmail.com [mailto:ccurvey@gmail.com] On Behalf Of Chris Curvey
Sent: Thursday, March 27, 2014 3:08 PM
To: Khangelani Gama; pgsql
Subject: Re: [GENERAL] Synchronizing a table that is two different databases : Need to dump a table a insert from db1 and change the insert statements into UPDATE statements

On Thu, Mar 27, 2014 at 6:16 AM, Khangelani Gama <kgama@argility.com> wrote:

Hi all

Synchronizing a table that is in two different databases(e.g db1 and db2).

Please help me with this. I need to dump a table as INSERTS from db1 (postgres 9.1.2) and change the INSERT statements into UPDATE statements in order to apply that change in db2(postgres 8.3.0.112) which has the same table as db1. Where the record does not exist I need to insert that record. There is more than 1000 INSERTS I need to convert to UPDATES for the same table. Please help .

Example:

Below it’s the INSERT from postgres 9.1.2 DATABASE which should convert to UPDATE statement.

INSERT INTO table (br_cde, br_desc, br_active, br_closed, grp_cde, ctry_cde, state_cde, br_addr_line1, br_addr_line2, br_addr_line3, br_addr_line4, br_addr_cde, br_post_addr_line1, br_post_addr_line2, br_post_addr_line3, br_post_addr_line4, br_post_addr_cde, br_phone, tax_cde, br_server_name, br_lang_ind, bureau_cde, br_risk_typ_cde, br_access_ccd, br_access_bureau, br_network_active, br_desc_short, br_is_trading, br_is_test, br_is_nomodem, br_is_whs, br_is_merch, br_is_cco, br_is_central, br_is_merch_active, br_central_brn, br_merch_brn, br_cco_brn, br_cgo_brn, br_cluster_brn, br_contact, br_tollfree, br_fax, br_telex, br_email, ops_cde, ops_director, br_cmpy_reg, br_tax_reg, audit_id, comp_cde, br_batch, br_release_pilot_type, br_is_order_active, bnk_id, br_class_cde, br_erp_xref, br_to_open, br_to_close, br_comments, br_is_hire, br_is_voucher, br_is_clear_center, br_is_headoffice, br_is_nsp_active, usr_pass_history, br_network_protocol, br_gen_ins_central_brn, br_cpi_ins_central_brn, br_rsc_area, br_rsc_perc, br_switch_deduc, br_tvlic_dealer_no, br_cco_followup, br_triad_live, br_do_cust_locking, br_is_rainbow, br_nca_live, br_ncr_reg_no, prov_cde, br_is_data_archive, br_icu_brn, br_living_expenses_live, br_ab_dorder_live, br_ab_receipts_active, br_spiv_max_alloc_perc, br_merger_brn, br_dep_bnk_acc, br_merchant_number, br_goods_ins_live, br_cgs_connection, whs_cde, br_crm_brn, subscription_active, br_prn_doc_default_active, br_use_jms, br_sso_active, br_paym8_properties, creditors_supp_no_prefix, br_block_debtors_obo, ccms_branch, br_is_ccms_live, ceres_conv_flag, cims_branch, br_is_cims_live, br_accept_sql_releases) VALUES ('9940', 'ELLERINES CENTRAL - 9940', true, NULL, '1', 'BAF', 'BAF', '209 SMIT STREET', 'BRA', 'Jgf', 'Jrg', '3025', '209 SMIT STREET', 'BRA', 'Jrg', NULL, '2017', '(4562) 712 1300', '1', 'dfgike.com', 'ENG', 'ITC', 'L', false, false, false, 'BATCH - 9940', false, false, false, false, false, false, true, false, '9940', NULL, NULL, '10.52.1.31', '9940', 'DOUG', NULL, '(4562) 712 1300' ‘, NULL, NULL, '001', NULL, '1969/02687/07', NULL, 0, NULL, '9940', NULL, false, NULL, NULL, NULL, NULL, NULL, NULL, false, false, false, false, false, 30, 'WS2~WS2', '1002', '1002', NULL, NULL, false, NULL, NULL, false, true, false, true, 'NC', NULL, true, NULL, true, true, NULL, NULL, NULL, NULL, NULL, true, 'oracle_live', NULL, NULL, true, NULL, true, false, NULL, NULL, NULL, NULL, NULL, NULL, NULL, false, false);

(For clarity, I'm going to call the table in question "foo".)

Option 1: If you just want to do a full copy of db1.foo to db2.foo, then do that. Dump foo from db1, truncate foo in db2 and load db2.foo from the dump. But you might not be able to do that (foreign keys and such). In that case..

Option 2: Dump the table from db1, then load it into a table with a different name ("bar") in db2. Then you can do this:

begin;

update foo

set br_desc = bar.br_desc

, br_active = bar.br_active

(rest of columns)

where foo.br_cde = bar.br_cde;

insert into foo (br_cde, br_desc, br_active, ...)

select br_cde, br_desc, br_active, ....

from bar

where not exists

( select *

from foo

where foo.br_cde = bar.br_cde);

commit;

CONFIDENTIALITY NOTICE
The contents of and attachments to this e-mail are intended for the addressee only, and may contain the confidential
information of Argility (Proprietary) Limited and/or its subsidiaries. Any review, use or dissemination thereof by anyone
other than the intended addressee is prohibited.If you are not the intended addressee please notify the writer immediately
and destroy the e-mail. Argility (Proprietary) Limited and its subsidiaries distance themselves from and accept no liability
for unauthorised use of their e-mail facilities or e-mails sent other than strictly for business purposes.