python + postgres psql + os.popen

D

damacy

hello, everyone.

i am trying to write a program which executes SQL commands stored in
..sql files.

i wrote a function called psql() whose contents look like the
following.

....
os.popen(command)
file = os.popen(command, 'w')
file.write(password)
file.close()
....

where command looks like
psql -h [host] -d [dbname] -U [username] -W -f "[filename]"

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?
 
L

Laszlo Nagy

damacy írta:
hello, everyone.

i am trying to write a program which executes SQL commands stored in
.sql files.

i wrote a function called psql() whose contents look like the
following.

...
os.popen(command)
file = os.popen(command, 'w')
file.write(password)
file.close()
...

where command looks like
psql -h [host] -d [dbname] -U [username] -W -f "[filename]"

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?
You can put this in the beginning of your SQL file:

\set ON_ERROR_STOP

If you also want to know what command caused the error:

\set ECHO all

You can also use a library written for Python. For example, psycopg

http://initd.org/projects/psycopg1

Best,

Laszlo
 
B

Bruno Desthuilliers

damacy said:
hello, everyone.

i am trying to write a program which executes SQL commands stored in
.sql files.

i wrote a function called psql() whose contents look like the
following.

...
os.popen(command)
file = os.popen(command, 'w')
file.write(password)
file.close()
...

where command looks like
psql -h [host] -d [dbname] -U [username] -W -f "[filename]"

this works well.

But is a very strange way to access a RDBMS from Python code. Are you
aware of the existence of db modules ?
can anyone help me?

http://www.python.org/dev/peps/pep-0249/
http://initd.org/projects/psycopg1
 
S

Simon Forman

damacy said:
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
 
D

damacy

hi, there. thanks for the help.

now i have a different problem now. i decided to use 'subprocess' and
'Popen' objects instead of 'os.popen()' function, which i believe do
not make much difference.

my code is like the following...

[1] link = subprocess.Popen(command, stdin = subprocess.PIPE, stdout =
subprocess.PIPE, stderr = subprocess.PIPE, shell = True)
[2] link.communicate(password)
[3] link.wait()
[4] err = link.communicate()[1]
[5] if err != None: print str(err)

i have read several threads about 'subprocess' posted on this group and
still i have way too much confusion regarding the above section of
code.

1. i'm currently using MS Windows.
i remember some have said that communicate() function is not usable on
this OS.
could anyone confirm this?

2. i'm expecting an error message, as i am trying to create a table
which does already exist in the database.
but if i try to print out the error message as [5], it is just an EMPTY
string.
and, if i try the SAME THING using command-line, i get a correct error
message this time ('psql:createstudent.sql:12: ERROR: relation
"student" already exists').

HOWEVER, if i comment out [2] link.communicate(password), meaning i do
not supply a password, it shows an error message, 'psql: fe_sendauth:
no password supplied', which is correct as expected.

my question is...
why does it work (i.e. showing a correct error message) when no
password supplied but NOT when creating a table which already exists in
the database? it should work for both cases.

thank you very much.
 
D

damacy

hi, there. thanks for the help.

now i have a different problem now. i decided to use 'subprocess' and
'Popen' objects instead of 'os.popen()' function, which i believe do
not make much difference.

my code is like the following...

[1] link = subprocess.Popen(command, stdin = subprocess.PIPE, stdout =
subprocess.PIPE, stderr = subprocess.PIPE, shell = True)
[2] link.communicate(password)
[3] link.wait()
[4] err = link.communicate()[1]
[5] if err != None: print str(err)

i have read several threads about 'subprocess' posted on this group and
still i have way too much confusion regarding the above section of
code.

1. i'm currently using MS Windows.
i remember some have said that communicate() function is not usable on
this OS.
could anyone confirm this?

2. i'm expecting an error message, as i am trying to create a table
which does already exist in the database.
but if i try to print out the error message as [5], it is just an EMPTY
string.
and, if i try the SAME THING using command-line, i get a correct error
message this time ('psql:createstudent.sql:12: ERROR: relation
"student" already exists').

HOWEVER, if i comment out [2] link.communicate(password), meaning i do
not supply a password, it shows an error message, 'psql: fe_sendauth:
no password supplied', which is correct as expected.

my question is...
why does it work (i.e. showing a correct error message) when no
password supplied but NOT when creating a table which already exists in
the database? it should work for both cases.

thank you very much.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top