I created a trigger to replicate inserts from a table 'mytable_db1' in database1 into the same table 'mytable_db2' on database2. Both databases are on the same server.
CREATE OR REPLACE FUNCTION trigger_osm_test_insert()
RETURNS trigger AS
$BODY$
BEGIN
PERFORM dblink_connect('db2', 'dbname=xxx port=5432 user=myusr password=xxx');
PERFORM dblink_exec('db2',
'insert into test.mytable_db2 (osm_id, name, name_eng, name_int, type, z_order, population, last_update, country, iso3, shape)
values ('||new.osm_id||', '''||new.name||''', '''||new.name_eng||''', '''||new.name_int||''', '''||new.type||''', '||new.z_order||',
'||new.population||', '''||new.last_update||''', '''||new.country||''', '''||new.iso3||''',
st_geometry((st_AsText('''||new.shape::text||'''))))');
PERFORM dblink_disconnect('db2');
RETURN new;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION trigger_osm_test_insert()
OWNER TO myusr;
CREATE TRIGGER osm_insert_test
AFTER INSERT
ON mytable_db1
FOR EACH ROW
EXECUTE PROCEDURE trigger_osm_test_insert();
However, when I make a test insert such as:
insert into test.mytable_db1 (name, shape) values ('test', '0101000020E6100000E0979950035F4A40404B2751B0861CC0');
The inserted row is inserted into mytable_db1, but the trigger seem not being working as I have nothing in mytable_db2. The insert doesn't give me any error message from the trigger.
I'm using postgresql 9.2.2. Both databases have dblink 1.0 installed as well as postgis 2.0.6.
Does anyone have suggestions on what I am doing wrong?
Thanks!