Thread: Seeking PyGreSQL help

Seeking PyGreSQL help

From
"Alastair G. Hogge"
Date:
Hello,

I'm running PostgreSQL-7.3.3_1 on FreeBSD 5.0-CURRENT. I'm trying to use 
python to interface with my database. I'm using Python-2.3 and 
py23-PyGreSQL-7.3.3_1 from the FreeBSD ports tree.

My problem is when I try to connect to the database.
Following is how the problem occurs:
<error>
#python
>>>import _pg
>>>db = _pg.connect("mydb", "myhost")
Traceback (most recent call last): File "<stdin>", line 1, in ?
_pg.error: could not connect to server: Connection refused       Is the server running on host myhost and accepting
 TCP/IP connections on port 5432?
 
</erro>

I know my database is working because when I load it up with psql all appears 
ok. I can even add and retrive information.

I'm only just starting out in Python and PostgreSQL so any help would be much 
appreciated.

Thanks in advance
-Al



Re: Seeking PyGreSQL help

From
"Alastair G. Hogge"
Date:
On Tuesday, 05 August 2003 19:10, D'Arcy J.M. Cain wrote:
> On Tuesday 05 August 2003 04:16, Alastair G. Hogge wrote:
> > I'm running PostgreSQL-7.3.3_1 on FreeBSD 5.0-CURRENT. I'm trying to use
> > python to interface with my database. I'm using Python-2.3 and
> > py23-PyGreSQL-7.3.3_1 from the FreeBSD ports tree.
> >
> > My problem is when I try to connect to the database.
> > Following is how the problem occurs:
> > <error>
> > #python
> >
> > >>>import _pg
> > >>>db = _pg.connect("mydb", "myhost")
> >
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in ?
> > _pg.error: could not connect to server: Connection refused
> >         Is the server running on host myhost and accepting
> >         TCP/IP connections on port 5432?
>
> What happens if you do 'db = _pg.connect("mydb")' from python or 'psql -h
> myhost' from the command line?  I suspect that your problem will reverse. 
> Bt default PostgreSQL does not run on a TCP/IP port.  You have to turn that
> on from the config file.  Don't forget pg_hba.conf if you change that.
Hello Darcy,

Thanks for help. The problem was in pgsql/data/postgresql.conf. I needed to 
change "#tcpip_socket = false" to "tcpip_socket = true"
I know am able to access my database from within Python, hwover when I try the 
python an cgi script I get an internet server error. Is there more options I 
should know about?





Re: Seeking PyGreSQL help

From
"Alastair G. Hogge"
Date:
On Tuesday, 05 August 2003 20:09, Alastair G. Hogge wrote:
OK, I've found my problem.
my code:
form = cgi.FieldStorage() # Grab data from web page form
db = _pg.connect("mydb", "myhost") # connect to database
temp = form['name'].value
db.query("INSERT INTO test VALUES (temp, 'none')")
db.close()

The following is to show that data has been recived from the web form.
Which works. temp will display what ever I type in the form, however, when I 
try to store it in the database above I get problems.
print "Content-Type: text/html\n\n"
print temp 

"db.query("INSERT INTO test VALUES (temp, 'none')")" is the line causing the 
problem. The internel server error from apache.



Re: Seeking PyGreSQL help

From
"Alastair G. Hogge"
Date:
On Tuesday, 05 August 2003 22:45, D'Arcy J.M. Cain wrote:
> On Tuesday 05 August 2003 08:41, Alastair G. Hogge wrote:
> > "db.query("INSERT INTO test VALUES (temp, 'none')")" is the line causing
> > the problem. The internel server error from apache.
>
> What happens when you try doing that line directly into psql?  I don't know
> what your schema for test is but perhaps what you want is this:
>
> db.query("INSERT INTO test VALUES (temp, NULL)")
In pyhton?
temp = "hello"
>>> db.query("INSERT INTO test VALUES (temp, NULL)")
Traceback (most recent call last): File "<stdin>", line 1, in ?
_pg.error: ERROR:  Attribute "temp" not found



Re: Seeking PyGreSQL help

From
"D'Arcy J.M. Cain"
Date:
On Tuesday 05 August 2003 04:16, Alastair G. Hogge wrote:
> I'm running PostgreSQL-7.3.3_1 on FreeBSD 5.0-CURRENT. I'm trying to use
> python to interface with my database. I'm using Python-2.3 and
> py23-PyGreSQL-7.3.3_1 from the FreeBSD ports tree.
>
> My problem is when I try to connect to the database.
> Following is how the problem occurs:
> <error>
> #python
>
> >>>import _pg
> >>>db = _pg.connect("mydb", "myhost")
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> _pg.error: could not connect to server: Connection refused
>         Is the server running on host myhost and accepting
>         TCP/IP connections on port 5432?

What happens if you do 'db = _pg.connect("mydb")' from python or 'psql -h 
myhost' from the command line?  I suspect that your problem will reverse.  Bt 
default PostgreSQL does not run on a TCP/IP port.  You have to turn that on 
from the config file.  Don't forget pg_hba.conf if you change that.

-- 
D'Arcy J.M. Cain
PyGreSQL Development Group
http://www.PyGreSQL.org


Re: Seeking PyGreSQL help

From
"D'Arcy J.M. Cain"
Date:
On Tuesday 05 August 2003 06:09, Alastair G. Hogge wrote:
> Thanks for help. The problem was in pgsql/data/postgresql.conf. I needed to
> change "#tcpip_socket = false" to "tcpip_socket = true"
> I know am able to access my database from within Python, hwover when I try
> the python an cgi script I get an internet server error. Is there more
> options I should know about?

I assume that you mean "Internal Server Error" or ISE.  Whenever your web
server gives an ISE there is something in the error log that tells you what
the problem is.  With Python you will get the exact line with the problem.
From there it is simpy a matter of debugging your code.

If you can't get access to the error logs for whatever reason you can add the
following line to your Python script to see the error in your browser.

import sys
sys.stderr = sys.stdout

You may have to view the source to see the error properly as your browser will
try to "format" it for you.

--
D'Arcy J.M. Cain
PyGreSQL Development Group
http://www.PyGreSQL.org


Re: Seeking PyGreSQL help

From
"Alastair G. Hogge"
Date:
On Wednesday, 06 August 2003 02:44, D'Arcy J.M. Cain wrote:
> On Tuesday 05 August 2003 08:53, Alastair G. Hogge wrote:
> > In pyhton?
> > temp = "hello"
> >
> > >>> db.query("INSERT INTO test VALUES (temp, NULL)")
> >
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in ?
> > _pg.error: ERROR:  Attribute "temp" not found
>
> Right.  That's the error.  You are trying to pass a Python variable name to
> the PostgreSQL backend.  Go to the Python page and look up formatting with
> the '%' operator.
Ah I see. Thanks for that.
All is working ok now. :-)
-Al





Re: Seeking PyGreSQL help

From
"D'Arcy J.M. Cain"
Date:
On Tuesday 05 August 2003 08:41, Alastair G. Hogge wrote:
> "db.query("INSERT INTO test VALUES (temp, 'none')")" is the line causing
> the problem. The internel server error from apache.

What happens when you try doing that line directly into psql?  I don't know 
what your schema for test is but perhaps what you want is this:

db.query("INSERT INTO test VALUES (temp, NULL)")

-- 
D'Arcy J.M. Cain
PyGreSQL Development Group
http://www.PyGreSQL.org


Re: Seeking PyGreSQL help

From
"D'Arcy J.M. Cain"
Date:
On Tuesday 05 August 2003 08:53, Alastair G. Hogge wrote:
> In pyhton?
> temp = "hello"
>
> >>> db.query("INSERT INTO test VALUES (temp, NULL)")
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> _pg.error: ERROR:  Attribute "temp" not found

Right.  That's the error.  You are trying to pass a Python variable name to 
the PostgreSQL backend.  Go to the Python page and look up formatting with 
the '%' operator.

-- 
D'Arcy J.M. Cain
PyGreSQL Development Group
http://www.PyGreSQL.org