Q:

How to parse psql output with subprocess.Popen

I want to execute the command psql --username=fred myDB < //10.0.0.1/share/dump.sql.pgdump but through subprocess.Popen, so that I can parse the output. However, I get the following error when doing so:
>>> psql: warning: extra command-line argument "<" ignored
>>> psql: warning: extra command-line argument "//10.0.0.1/share/dump.sql.pgdump" ignored

It seems I'm not formatting the command correctly, but I don't understand why. This is what I have so far:
PASS = 'mypassword'
os.putenv('PGPASSWORD', PASS)
latest_dump = '//10.0.0.1/share/dump.sql.pgdump'
cmd = ['psql', '--username=fred', 'myDB', '<', latest_dump]
p = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT )

# Print each line
for line in iter(p.stdout.readline, b''):
    sys.stdout.flush()
    print(">>> " + line.rstrip())

A:

The use of < redirector is a functionality of the shell. You can use the shell=True keyword argument if your values are safe (meaning they cannot be under the control of an untrusted user):
cmd = 'psql --username=fred myDB < %s' % latest_dump
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)

A better way to do this would be:
cmd = ['psql', '--username=fred', 'myDB']
p = subprocess.Popen(cmd, stdin=open(latest_dump),
                     stdout=subprocess.PIPE,
                     stderr=subprocess.STDOUT)

And of course you probably want to use a with open(latest_dump) as ...: block to open the file.

