Python 与Postgresql的连接被拒绝

Python 与Postgresql的连接被拒绝,python,postgresql,Python,Postgresql,下面是我为使用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") cu

下面是我为使用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?
OperationalError回溯(最近一次调用)
在()
1导入psycopg2
2.
---->3连接=psycopg2.connect(dbname=“gps\U热图”,user=“postgres”,host=“localhost”,password=“1234”,port=“5432”)
4 cursor=connection.cursor()
5.
C:\Users\******\Anaconda3\lib\site packages\psycopg2\\ uuuu init\ uuuuuuuuu.py in connect(dsn、数据库、用户、密码、主机、端口、连接工厂、游标工厂、异步、**kwargs)
162(k,v)项])
163
-->164连接=连接(dsn,连接工厂=连接工厂,异步=异步)
165如果游标工厂不是无:
166连接游标工厂=游标工厂
操作错误:无法连接到服务器:连接被拒绝(0x0000274D/10061)
服务器是否在主机“localhost”(::1)上运行并接受
端口5432上的TCP/IP连接?
无法连接到服务器:连接被拒绝(0x0000274D/10061)
服务器是否在主机“localhost”(127.0.0.1)上运行并接受
端口5432上的TCP/IP连接?
我将pg_hbf.conf编辑为: 托管所有0.0.0.0/0 md5


同样,重复了相同的错误

澄清

下面的答案被接受了,但是,这不是问题的解决方案。。。问题是postgresql server配置为端口5433,而不是默认端口5432。这应该可以解决问题:

connection = psycopg2.connect(database="gps_heatmap", user="postgres", password="1234", host="localhost", port=5433)

原始答案

尝试将
dbname=“gps\u heatmap”
替换为
database=“gps\u heatmap”
,因为前者用于连接字符串,后者用于将关键字参数传递给
psycopg2.connect()

或者可以使用连接字符串:

connection = psycopg2.connect("dbname=gps_heatmap user=postgres host=localhost password=1234")

对我来说,有效的方法是打开服务(在Windows中搜索),然后查找postgres服务:在我的例子中是postgresql-x64-10

  • 运行服务
  • 重新启动服务
  • 之后,错误就消失了


    我的错误源于在PostgreSQL之后安装MySQL,我猜端口或其他方面发生了变化,但这解决了问题。

    我尝试了这两种方法,但出现了相同的错误。有没有办法让服务器在本地主机上运行并建立TCP连接?我刚刚打开了psql和pgadmin。数据库gps_热图是使用psql创建的。值得一试。是的,你可以用TCP连接。您是否已检查postgres服务器正在哪个端口上运行?(默认为5432)。使用psql时是否指定主机名(localhost)?(它可能正在使用unix域套接字)。成功连接的
    psql
    命令是什么?我知道了。实际上,端口号有错误。它是5433,但代码默认为5432
    connection = psycopg2.connect("dbname=gps_heatmap user=postgres host=localhost password=1234")