Thread: creating a trigger to access another postgres database?

creating a trigger to access another postgres database?

From
carty mc
Date:
I have a question regarding postgres Trigger.
We have two applications which connect to two different databases (Both are postgres).
Is it possible to create a trigger (row based) in one database say A, which can access another database say B and updates a table there.
 
In brief, Is it possible to create Trigger in one database, which can connect to another database for making an update in the other database table.
 
thanks,


Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.

Re: creating a trigger to access another postgres database?

From
Richard Huxton
Date:
carty mc wrote:
> I have a question regarding postgres Trigger. We have two
> applications which connect to two different databases (Both are
> postgres). Is it possible to create a trigger (row based) in one
> database say A, which can access another database say B and updates a
> table there.

Sure - check the manuals (in 8.3) for dblink - it's in contrib. It's
been there for a while, but the documentation for contrib modules wasn't
in the manual until 8.3

--
   Richard Huxton
   Archonet Ltd

Re: creating a trigger to access another postgres database?

From
carty mc
Date:
Thanks Richard,
I went through dblink and tried it . But I am not able to pass variables to sql stmt of dblink_exec function . Basically If I hardcode the values for updating a table for the sql statement in different database it is working fine. But What I wanted to do is get the info from existing modified row and pass that into the update query which I am writing inside dblink_exec call.
 
my sql inside dblink_exec call would look like
UPDATE my_table set myotherDBTableField =  NEW.currDBTableField WHERE {condition}
 
When the trigger function executes this it is giving an error some Rule need to be set.
Whereas If I hardcode that value instead of using NEW.xxxx it is working fine?
 
I appreciate any insights,
 
thanks, 
 
 

Richard Huxton <dev@archonet.com> wrote:
carty mc wrote:
> I have a question regarding postgres Trigger. We have two
> applications which connect to two different databases (Both are
> postgres). Is it possible to create a trigger (row based) in one
> database say A, which can access another database say B and updates a
> table there.

Sure - check the manuals (in 8.3) for dblink - it's in contrib. It's
been there for a while, but the documentation for contrib modules wasn't
in the manual until 8.3

--
Richard Huxton
Archonet Ltd


No Cost - Get a month of Blockbuster Total Access now. Sweet deal for Yahoo! users and friends.

Re: creating a trigger to access another postgres database?

From
Richard Huxton
Date:
carty mc wrote:
> Thanks Richard, I went through dblink and tried it . But I am not
> able to pass variables to sql stmt of dblink_exec function .
> Basically If I hardcode the values for updating a table for the sql
> statement in different database it is working fine. But What I wanted
> to do is get the info from existing modified row and pass that into
> the update query which I am writing inside dblink_exec call.
>
> my sql inside dblink_exec call would look like UPDATE my_table set
> myotherDBTableField =  NEW.currDBTableField WHERE {condition}

dblink takes SQL as text - it's really just another external client as
far as the other database is concerned. You'll want to build your
query-string as a string:

mySql := 'UPDATE mytable SET f = ' || quote_literal(NEW.f) || ' WHERE ...'
dblink(connstr, mySql);

Don't forget to consider how NULLs will affect the query-string. See the
string-functions section of the manuals for quote_literal/quote_ident.

--
   Richard Huxton
   Archonet Ltd

Re: creating a trigger to access another postgres database?

From
carty mc
Date:
Thank you very much Richard. It worked like a charm.
I Appreciate your help
  

Richard Huxton <dev@archonet.com> wrote:
carty mc wrote:
> Thanks Richard, I went through dblink and tried it . But I am not
> able to pass variables to sql stmt of dblink_exec function .
> Basically If I hardcode the values for updating a table for the sql
> statement in different database it is working fine. But What I wanted
> to do is get the info from existing modified row and pass that into
> the update query which I am writing inside dblink_exec call.
>
> my sql inside dblink_exec call would look like UPDATE my_table set
> myotherDBTableField = NEW.currDBTableField WHERE {condition}

dblink takes SQL as text - it's really just another external client as
far as the other database is concerned. You'll want to build your
query-string as a string:

mySql := 'UPDATE mytable SET f = ' || quote_literal(NEW.f) || ' WHERE ...'
dblink(connstr, mySql);

Don't forget to consider how NULLs will affect the query-string. See the
string-functions section of the manuals for quote_literal/quote_ident.

--
Richard Huxton
Archonet Ltd


Looking for last minute shopping deals? Find them fast with Yahoo! Search.