从 PostgreSQL 触发器调用外部程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21634848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling external program from PostgreSQL trigger
提问by Thit Lwin Oo
I would like to execute external program (such as .net c# console) when PostgreSQL trigger is fired. How can I achieve it?
当 PostgreSQL 触发器被触发时,我想执行外部程序(例如 .net c# 控制台)。我怎样才能实现它?
回答by Erwin Brandstetter
Postgres cannot normally run external programs for security reasons.
出于安全原因,Postgres 通常不能运行外部程序。
The typical solution is to use NOTIFY
and have a daemon LISTEN
to it. There are solutions for every major scripting language out there ...
典型的解决方案是使用NOTIFY
并拥有一个守护进程LISTEN
。每种主要的脚本语言都有解决方案......
Examples for Java from @Craig: How to refresh JPA entities when backend database changes asynchronously?
来自@Craig 的 Java 示例: 如何在后端数据库异步更改时刷新 JPA 实体?
回答by pmenke
Since Postgres 9.3 there is a solution for invoking external programs. It is - for security reasons - limited to superusers and IMHO intended for exporting data, rather than doing a "notification on trigger":
从 Postgres 9.3 开始,有一个调用外部程序的解决方案。它 - 出于安全原因 - 仅限于用于导出数据的超级用户和恕我直言,而不是执行“触发通知”:
COPY (SELECT 1) TO PROGRAM '/bin/touch /tmp/created_by_postgres'
If you want to actually export data to the invoked programm, you can provide any SELECT or a table name instead of SELECT 1. The query results will then be passed to the invoked program via its standard input.
如果您想实际将数据导出到调用的程序,您可以提供任何 SELECT 或表名而不是SELECT 1。然后查询结果将通过其标准输入传递给被调用的程序。
You can find documentation of the feature in the Postgres docs: http://www.postgresql.org/docs/9.3/static/sql-copy.html
您可以在 Postgres 文档中找到该功能的文档:http: //www.postgresql.org/docs/9.3/static/sql-copy.html