在Postgresql中的触发器上运行外部程序 [英] Run external program on trigger in Postgresql
问题描述
将新行添加到我们的Postgres数据库时,我想运行bash脚本。 PostgreSQL中是否有任何 system()
函数可以让我做到这一点?
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?
要提供更多功能在上下文中,当在数据库中创建新行时,我需要通过SSH在外部服务器上创建一个包含该行信息的新文件。
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.
只是每隔5分钟左右运行一次cron作业并检查是否有新行,但是如果有人先删除一行然后添加一行,它可能就无法捕获。
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.
推荐答案
似乎无法执行此操作。我能找到的最简单的方法是使用Postgres的NOTIFY命令,然后用其他某种语言编写一个守护程序来监听那些通知。
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.
关于这些通知的最酷的事情是,您可以随它们发送一个有效负载,其中可以包含您想要的任何内容,因此您可以发送 ROW CREATED:ID# 455029,然后您的守护程序将知道选择该行以获取有关此行的更多信息。 Neato
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
这篇关于在Postgresql中的触发器上运行外部程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!