2

I'm running into a weird problem where I'm unable to connect to PostgreSQL from a Python 3.2 install. I'm running Fedora 15 and have installed Python3 and PostgerSQL9 from the Fedora repositories using yum. Does anyone have any ideas on why I'm seeing this problem and how to correct it? Google searches haven't been turning up anything.

I've changed the username, password, and database, but my pg_hba.conf file is correct.

import postgresql
t = postgresql.open(user='validuser', password='secret', database='some_database')

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.2/site-packages/postgresql/__init__.py", line 88, in open
    c.connect()
  File "/usr/lib64/python3.2/site-packages/postgresql/driver/pq3.py", line 2419, in connect
    pq = Connection3(sf, startup, password = password,)
  File "/usr/lib64/python3.2/site-packages/postgresql/protocol/client3.py", line 514, in __init__
    element.Startup(**startup), password
TypeError: keyword arguments must be strings

As a side note, I get this same error if I try to connect using different user, password, database combinations, and also if I use a pq://user:password@host/database connection string instead of keywords, both to localhost and to remote hosts.

thetaiko
  • 7,816
  • 2
  • 33
  • 49

3 Answers3

4

I think that probably is some bug in python3-postgresql package, but it looks like it works after little change. Edit this file (probably /usr/lib64 for 64 bit installations):

/usr/lib/python3.2/site-packages/postgresql/protocol/client3.py

Change (line 514):

element.Startup(**startup), password

to:

element.Startup(startup), password

After that I made simple connection (I changed pg_hba.conf host methods to md5) and it looks ok:

[grzegorz@localhost Desktop]$ python3
Python 3.2 (r32:88445, Feb 21 2011, 21:12:33) 
[GCC 4.6.0 20110212 (Red Hat 4.6.0-0.7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import postgresql
>>> db = postgresql.open("pq://grzegorz:12345@localhost/grzegorz")
>>> ps = db.prepare("SELECT version()")
>>> ps()
[('PostgreSQL 9.0.4 on i386-redhat-linux-gnu, compiled by GCC gcc (GCC) 4.6.0 20110530 (Red Hat 4.6.0-9), 32-bit',)]
>>> ps = db.prepare("TABLE t")
>>> ps()
[(1, 'aaa'), (2, 'bbb'), (3, 'ccc')]
>>>
Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
0

Just a guess, but python is probably passing unicode to postgres and it's expecting strings.

Gerrat
  • 28,863
  • 9
  • 73
  • 101
  • thanks for the response. After a bit more research I'm guessing that this is an incompatibility caused by an upgrade to Python3.2 from Python3.1. I'm hoping to find someone who has experienced the same issue and knows an easy fix. I suppose recompiling Python3.1 is worth a shot. – thetaiko Jul 13 '11 at 21:46
  • Yes sir. This is fixed in v1.0.2: "Fix Startup() for Python3.2's no non-str() keywords." – jwp Sep 15 '11 at 16:04
0

You can also use another module - psycopg2 to connect to postgresql

http://initd.org/psycopg/download/

w.diesel
  • 300
  • 3
  • 5