简体   繁体   中英

Run python or javascript file from postgres function?

How i can Run python or javascript file from postgres function ?

I have two postgres data base with totally different structure .

but i need to sync that two databases .

I will write the script that will read data from local data base and write into server data base in any language (python , java , javascript, node).

but the main problem is :

I do not know how to call that script from postgres procedure or function .

so the ques is :

How we can call or run any script from postgres procedure ?

Use plperl , plpython or pljava and use the system utils of the language. Ie in plpython use subprocess , in java use Runtime.getRuntime().exec and so on:

For example in 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();

CONSIDERATIONS

Actually you can launch a script from a Postgres function, but this is not the preferred way to synchronize data between databases. You probably will have issues related to absolute and relative paths to scripts and data, and file system permissions. Instead use a database function to export data in a known format and call your synchronize script from outside the database. Another options is use the dblink module.

您可以创建一个Perl函数,然后在其中调用脚本来同步数据库。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM