How can I send email from PostgreSQL trigger?

Viewed 51530

I using pgsql to set a trigger, when update the table dataset(change the status to Finished) it will automatic send a email to the email account using dataset email value and save this email in server

but i don't know how to write in trigger function to send email, and send email in server. Thank you in advance

Pg version is 9.1, and CentOS 5.8

CREATE OR REPLACE FUNCTION sss()
RETURNS trigger AS
$BODY$begin
if(NEW.publisher== 'aaaa')
then
//send email and save to server 192.168.171.64
end if;
return NEW;
end

$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION sss()
OWNER TO postgres;
GRANT EXECUTE ON FUNCTION sss() TO postgres;
6 Answers

If you use TypeScript/Node, then use the battle-tested graphile-worker codebase to communicate from Postgres to Node. Then you can easily use your standard email logic in Node to send a email.

Related