473,406 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

python + postgres psql + os.popen

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?

Jun 22 '06 #1
5 5880
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
Jun 22 '06 #2
damacy wrote:
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

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jun 22 '06 #3
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

Jun 22 '06 #4
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.
Simon Forman wrote:
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


Jun 24 '06 #5
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.
Simon Forman wrote:
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


Jun 24 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Lang | last post by:
Hi to all, can some one point me to the correct way, how to use PostgreSQLs "COPY" feature from within python ? What i want to do is: connect start transaction drop current tablecontens
2
by: Xah Lee | last post by:
Python Doc Problem Example: os.system Xah Lee, 2005-09 today i'm trying to use Python to call shell commands. e.g. in Perl something like output=qx(ls) in Python i quickly located the...
48
by: Edwin Quijada | last post by:
Hi !! Everybody I am developing app using Delphi and I have a question: I have to save pictures into my database. Each picture has 20 o 30k aprox. What is the way more optimus? That 's table will...
0
by: phil campaigne | last post by:
scott.marlowe wrote: > On Thu, 4 Mar 2004, phil campaigne wrote: > > > >> Hello, >> when I login to linux and check the env's I see: >>...
6
by: Prabu Subroto | last post by:
Dear my friends... Usually I use MySQL. Now I have to migrate my database from MySQL to Postgres. I have created a database successfully with "creatdb" and a user account successfully. But...
0
by: Együd Csaba | last post by:
> -----Original Message----- > From: pgsql-admin-owner@postgresql.org > On Behalf Of Együd Csaba > Sent: 2004. július 16. 12:23 > To: pgsql-admin (E-mail) > Subject: URGENT - Postgres won't...
3
by: Darkcamel | last post by:
Hello all, I am new to postgres and don't really understand how the database is set-up. I am very fluent with mysql and sql2000, but postgres is new to me. If anyone can point me to some good...
7
by: damacy | last post by:
hi, there. i have this question which might sound quite stupid to some people, but here we go anyway. i have written a python program which interacts with a postgresql database. what it does is...
0
by: kishoramballi | last post by:
Hi, I have created table using following sql statement through psql tool in test database using create table t1(f1 int); table is created. Now I am using an odbc script to insert 5 records....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.