Python Not Calling An External Program Part 3
Solution 1:
It sounds like you want to invoke a command-line program from within a PostgreSQL trigger or function.
A usually-better alternative is to have the trigger send a NOTIFY
and have a process with a PostgreSQL connection LISTEN
ing for notifications. When a notification comes in, the process can start your program. This is the approach I would recommend; it's a lot cleaner and it means your program doesn't have to run under PostgreSQL's user ID. See NOTIFY
and LISTEN
.
If you really need to run commands from inside Pg:
You can use PL/Pythonu
with os.system
or subprocess.check_call
; PL/Perlu
with system()
; etc. All these can run commands from inside Pg if you need to. You can't invoke programs directly from PostgreSQL, you need to use one of the 'untrusted' (meaning fully privileged, not sandboxed) procedural languages to invoke external executables. PL/TCL can probably do it too.
Update:
Your Python code as shown above has several problems:
- Using
os.system
in Python to copy files is just wrong. Use theshutil
library: http://docs.python.org/3/library/shutil.html to copy files, and the simpleos.mkdir
command to create directories. - The double-layered quoting looks wrong; didn't you mean to quote only each argument not the whole command? You should be using
subprocess.call
instead ofos.system
anyway. - Your final
subprocess.call
invocation appears OK, but fails to check the error code so you'll never know if it went wrong; you should usesubprocess.check_call
instead.
The C++ code also appears to fail to check for errors from the system()
invocations so you'll never know if the command it runs fails.
Like the Python code, copying files in C++ by using the copy
shell command is generally wrong. Microsoft Windows provides the CopyFile
function for this; equivalents or alternatives exist on other platforms and you can use portable-but-less-efficient stream copying too.
Post a Comment for "Python Not Calling An External Program Part 3"