execute pg_dump via python

Started by Garry Saddingtonover 18 years ago3 messagesgeneral
Jump to latest
#1Garry Saddington
garry@schoolteachers.co.uk

I am using zope on windows with an external python method to backup my
database. I am struggling to run the following command:

pg_dump.exe database > file

I have tried using os.popen - no luck
and also subprocess.Popen.

eg:
import subprocess

subprocess.Popen(['c:/dir/dir/pg_dump.exe','database','>','c:/dir/dir/output
file'])

The command string works perfectly in a terminal. Does anyone know how I
should be doing this? I get no errors or traceback when I try the method
through Zope.

regards
garry

#2Andy
nospam@noplace.com
In reply to: Garry Saddington (#1)
Re: execute pg_dump via python

Garry Saddington wrote:

I am using zope on windows with an external python method to backup my
database. I am struggling to run the following command:

pg_dump.exe database > file

I have tried using os.popen - no luck
and also subprocess.Popen.

eg:
import subprocess

subprocess.Popen(['c:/dir/dir/pg_dump.exe','database','>','c:/dir/dir/output
file'])

The command string works perfectly in a terminal. Does anyone know how I
should be doing this? I get no errors or traceback when I try the method
through Zope.

regards
garry

the redirect operation '>' is done by cmd.exe. I assume popen is
directly spawing pg_dump, but pg_dump does not know the command line
argument >.

you need to run: 'cmd.exe /c pg_dump database > out.txt'

(is it /c or /k, I always get them confused)

-Andy

#3Trevor Talbot
quension@gmail.com
In reply to: Garry Saddington (#1)
Re: execute pg_dump via python

On 10/25/07, Garry Saddington <garry@schoolteachers.co.uk> wrote:

I am using zope on windows with an external python method to backup my
database. I am struggling to run the following command:

pg_dump.exe database > file

subprocess.Popen(['c:/dir/dir/pg_dump.exe','database','>','c:/dir/dir/output
file'])

The command string works perfectly in a terminal. Does anyone know how I
should be doing this? I get no errors or traceback when I try the method
through Zope.

This is probably a Python question more than anything else. I don't
know Python, but two things come to mind:

* It probably does not open a command shell, so file redirection ('>')
does not work. Use pg_dump's -f option instead.

* Things named "popen" usually open a pair of pipes for programmatic
input and output, meaning your app is expected to read the output of
pg_dump directly, as if you were going to display it on the screen or
write to a file yourself.

I'd suggest asking in a Python group about executing external
processes and checking for errors.