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
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.
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