【问题标题】:Run python or javascript file from postgres function?从 postgres 函数运行 python 或 javascript 文件?
【发布时间】:2016-01-23 21:21:16
【问题描述】:

如何从 postgres 函数运行 python 或 javascript 文件?

我有两个结构完全不同的 postgres 数据库。

但我需要同步这两个数据库。

我将编写从本地数据库读取数据并以任何语言(python、java、javascript、节点)写入服务器数据库的脚本。

但主要问题是:

我不知道如何从 postgres 过程或函数中调用该脚本。

所以问题是:

我们如何从 postgres 程序调用或运行任何脚本?

【问题讨论】:

    标签: javascript python postgresql


    【解决方案1】:

    使用plperlplpythonpljava 并使用该语言的系统实用程序。即在plpython中使用subprocess,在java中使用Runtime.getRuntime().exec等等:

    例如在plpython:

    CREATE LANGUAGE plpythonu; -- create language if does not exist
    
    CREATE OR REPLACE FUNCTION venue.test_execute()
      RETURNS void AS
      $BODY$
      import subprocess
      subprocess.call(['/path/to/script/test.py'])
    
      $BODY$
    LANGUAGE plpythonu;
    
    SELECT venue.test_execute();
    

    注意事项

    实际上,您可以从 Postgres 函数启动脚本,但这不是在数据库之间同步数据的首选方式。您可能会遇到与脚本和数据的绝对和相对路径以及文件系统权限相关的问题。而是使用数据库函数以已知格式导出数据并从数据库外部调用同步脚本。另一种选择是使用dblink 模块。

    【讨论】:

    • 我正在尝试通过以下命令安装 plpythonu:sudo apt-get install postgresql-contrib-9.4 postgresql-plpython-9.4 但它不起作用
    • 使用此命令:aptitude search plpython 并检查您系统的可用软件包列表
    • 我有很多列表:postgres-xc-plpython - PL/Python 过程语言 fo p postgres-xc-plpython:i - PL/Python 过程语言 fo v postgresql-plpython - v postgresql-plpython: i3 -p postgresql-plpython-8。 - PL/Python 程序语言 fo 。 . . p postgresql-plpython-9。 - PL/Python 程序语言 fo 。 . . . v postgresql-plpython3:i -p postgresql-plpython3-9 - PL/Python 3 过程语言。 . . .
    • 我已经安装了包postgresql-plpython-9.3
    • 现在我需要做什么
    【解决方案2】:

    您可以创建一个Perl Function 并在其中调用您的脚本来同步数据库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-12
      • 1970-01-01
      相关资源
      最近更新 更多