这是我为使用psycopg2连接Postgresql而编写的代码。我的psql和pgadminIII也在运行。
import psycopg2
connection = psycopg2.connect(dbname="gps_heatmap",user="postgres",host="localhost",password="1234")
cursor = connection.cursor()
cursor.execute("DROP TABLE IF EXISTS roads")
cursor.execute("CREATE TABLE roads (" +
"id SERIAL PRIMARY KEY," +
"name VARCHAR," +
"centerline GEOMETRY)")
cursor.execute("CREATE INDEX ON roads USING GIST(centerline)")
connection.commit()
但会出现以下错误:
OperationalError Traceback (most recent call last)
<ipython-input-14-03e3f214b83e> in <module>()
1 import psycopg2
2
----> 3 connection = psycopg2.connect(dbname="gps_heatmap",user="postgres",host="localhost",password="1234",port="5432")
4 cursor = connection.cursor()
5
C:\Users\*******\Anaconda3\lib\site-packages\psycopg2\__init__.py in connect(dsn, database, user, password, host, port, connection_factory, cursor_factory, async, **kwargs)
162 for (k, v) in items])
163
--> 164 conn = _connect(dsn, connection_factory=connection_factory, async=async)
165 if cursor_factory is not None:
166 conn.cursor_factory = cursor_factory
OperationalError: could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
我将pg_hbf.conf编辑为: 全部托管0.0.0.0/0 md5
同样的错误再次出现
我的工作是打开服务(在Windows中搜索),然后查找postgres服务:postgresql-x64-10。
之后,错误就消失了。
我的错误源于在PostgreSQL之后安装MySQL,我想是端口或其他什么地方发生了变化,但这解决了它。
澄清
下面的答案被接受了,但是,这并不是解决问题的办法。。。问题是postgresql服务器配置为端口5433,而不是默认端口5432。这样可以解决问题:
原始答案
尝试将
dbname="gps_heatmap"
替换为database="gps_heatmap"
,因为前者用于连接字符串,后者用于将关键字参数传递给psycopg2.connect()
:或者可以使用连接字符串:
相关问题 更多 >
编程相关推荐