Run external program on trigger in Postgresql

2019-09-10 11:48发布

问题:

When a new row is added to our Postgres database, I want to run a bash script. Is there any sort of system() function in PostgreSQL that would allow me to do this?

To give more context, when a new row is created in the database, I need to make a new file containing information from that row on an external server via SSH.

An alternate solution would just be to have a cron job run every 5 minutes or so and check for new rows, but then it might not catch if someone deletes a row, and then adds a row.

回答1:

It looks like there is no way to do this. The simplest way I can find is to use Postgres' NOTIFY command, and then write a daemon in some other language to listen for those notifications.

The cool thing about those notifications is that you can send a 'payload' with them, which can include anything you want, so you could send "ROW CREATED: ID# 455029" and then your daemon would know to SELECT that row to get more info about it. Neato