Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法从Python3打开到PostgreSQL的连接_Python_Postgresql_Python 3.x_Py Postgresql - Fatal编程技术网

无法从Python3打开到PostgreSQL的连接

无法从Python3打开到PostgreSQL的连接,python,postgresql,python-3.x,py-postgresql,Python,Postgresql,Python 3.x,Py Postgresql,我遇到了一个奇怪的问题,无法从Python 3.2安装连接到PostgreSQL。我正在运行Fedora 15,并已使用yum从Fedora存储库安装了Python3和PostgerSQL9。有人知道我为什么会看到这个问题以及如何纠正它吗?谷歌搜索没有发现任何东西 我已经更改了用户名、密码和数据库,但我的pg_hba.conf文件是正确的 import postgresql t = postgresql.open(user='validuser', password='secret', data

我遇到了一个奇怪的问题,无法从Python 3.2安装连接到PostgreSQL。我正在运行Fedora 15,并已使用yum从Fedora存储库安装了Python3和PostgerSQL9。有人知道我为什么会看到这个问题以及如何纠正它吗?谷歌搜索没有发现任何东西

我已经更改了用户名、密码和数据库,但我的pg_hba.conf文件是正确的

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
导入postgresql
t=postgresql.open(user='validuser',password='secret',database='some_database')
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/usr/lib64/python3.2/site packages/postgresql/_init__.py”,第88行,打开
c、 连接()
文件“/usr/lib64/python3.2/site packages/postgresql/driver/pq3.py”,第2419行,在connect中
pq=连接3(sf,启动,密码=密码)
文件“/usr/lib64/python3.2/site packages/postgresql/protocol/client3.py”,第514行,在__
元素启动(**启动),密码
TypeError:关键字参数必须是字符串

作为旁注,如果我尝试使用不同的用户、密码、数据库组合进行连接,以及使用
pq://用户:password@host/数据库连接字符串而不是关键字,包括本地主机和远程主机。

只是猜测,但是python可能正在将unicode传递给postgres,它需要字符串

我认为这可能是软件包中的一些错误,但看起来在做了一些小改动后就可以正常工作了。编辑此文件(对于64位安装,可能是
/usr/lib64
):

更改(第514行):

致:

之后,我进行了简单的连接(我将
pg_hba.conf
host方法更改为md5),它看起来还可以:

[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')]
>>>

您还可以使用另一个模块psycopg2连接到postgresql


谢谢您的回复。经过进一步研究,我猜这是由于从Python3.1升级到Python3.2而导致的不兼容。我希望找到一个经历过同样问题的人,并且知道一个简单的解决方法。我想重新编译Python3.1是值得一试的。是的,先生。这在v1.0.2中得到了修复:“修复Python3.2中没有非-str()关键字的Startup()。”它与Python3兼容吗?
element.Startup(**startup), password
element.Startup(startup), password
[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')]
>>>