How can I catch an event of a new postgreSQL record with golang

How can I catch an event of a new postgreSQL record with golang



I have a script that connects to DB and can get data from it
Can I somehow make it to notify me, when any new record is added to the DB table






NOTIFY and friends might be of interest. AFAIK you'd need to set up insert-triggers on the tables you want to watch and I'm not sure how well any of this works with Go's PostgreSQL interface.

– mu is too short
Sep 12 '18 at 7:25






Take a look at Postgresql Listen/Notify feature. postgresql.org/docs/9.3/static/sql-listen.html You can create a listener which sends a NOTIFY-Message to a channel. You can listen on that channel in golang.

– Feralus
Sep 12 '18 at 9:46




2 Answers
2



As mentonied in comment section you can use LISTEN/NOTIFY Feature of Postgresql.
With Golang's Postgresql-Lib you can easily fetch notify-events and react to new database-events.
Here is a simple example of the go implementation: https://play.golang.org/p/hOsU89oC6fS
i used in a project.



Thank everyone for the answers



Solved this using sql triggers and go-pg library:



Created sql function called insert_test_func, that on INSERT(in my case) action do


PERFORM pg_notify('mychan', 'Message');



Created trigger, that executes func


create trigger check_insert
before insert or update on *my_table_name*
for each row
execute procedure insert_test_func();



And execute this trigger



With "github.com/go-pg/pg", connect to the DB and with pg.Listen(function ) listening to the channel for the 'Message'



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế