Creating trigger from one database table to another database table postgresql
-
03-03-2021 - |
Вопрос
I have two database say db1 and db2. And two tables user_auth_table in db1 and user_table in db2. Now I want to trigger a function which sync up few values in both tables for each new row or on each new insertion in user_auth_table.
I have tried to write on my own and creating trigger in db1. But the error says user_table doesn't exist. Which I know why I am getting this error. Any suggestion to target the tables outside the db ?
CREATE TRIGGER sync_user_table AFTER INSERT ON user_auth_table for each row execute procedure syncAndMaintainUsersTable()
create or replace function syncAndMaintainUsersTable()
returns trigger as
$BODY$
begin
if NEW.user_uuid<>OLD.user_uuid then
insert into user_table values (NEW.user_uuid, CURRENT_TIMESTAMP, NEW.provider);
end if;
return new;
end;
$BODY$
language plpgsql;
Решение
Usually the full qualified name of a db table is:
catalog.schema.table
If you want to insert some values into another table you should use this syntax:
insert into catalog.schema.table
values (..., ..., ...)
Не связан с dba.stackexchange