python + postgres psql + os.popen

Simon Forman rogue_pedro at yahoo.com
Thu Jun 22 12:07:44 EDT 2006


damacy wrote:
> hello, everyone.
...
> this works well. however, it does not show me any warning nor error
> messages if there is one. for example, i am trying to create a table
> which already exists in the database, it should show me a warning/error
> message saying there already is one present in the database, or
> something like that.
>
> can anyone help me?

I recently needed to use psql from python on a computer that I couldn't
install psycopg on and I used something similar to this to do it (I
edited the code slightly to make it clearer):

from subprocess import Popen, PIPE

# Pass the password through an environment
# variable to prevent psql asking for it.
psql_env = dict(PGPASSWORD='********')

# Create the subprocess.
proc = Popen(cmd, shell=True, env=psql_env, stdout=PIPE, stderr=PIPE)

# Try reading it's data.
data = proc.stdout.read()

# Check for errors.
err = proc.stderr.read()
if err: raise Exception(err)


It worked nicely for me, YMMV.


Hope that helps,

~Simon




More information about the Python-list mailing list