从 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:18:07  来源:igfitidea点击:

Calling external program from PostgreSQL trigger

postgresqlnotificationslistener

提问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 NOTIFYand have a daemon LISTENto it. There are solutions for every major scripting language out there ...

典型的解决方案是使用NOTIFY并拥有一个守护进程LISTEN。每种主要的脚本语言都有解决方案......

回答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