连接拒绝 Postgresql
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37386481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connection refused to Postgresql
提问by Himal Acharya
Here is the code I wrote for connecting Postgresql using psycopg2. My psql and pgadminIII is also running.
这是我为使用 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()
But following error comes:
但出现以下错误:
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?
I edited the pg_hbf.conf as: host all all 0.0.0.0/0 md5
我将 pg_hbf.conf 编辑为:host all all 0.0.0.0/0 md5
Again,same error repeated
再次,同样的错误重复
采纳答案by mhawke
Clarification
澄清
The answer below was accepted, however, it was not the solution to the problem... the problem was that the postgresql server was configured for port 5433, not the default port of 5432. This should fix the problem:
下面的答案被接受了,但是,它不是问题的解决方案......问题是postgresql服务器被配置为端口5433,而不是默认端口5432。这应该可以解决问题:
connection = psycopg2.connect(database="gps_heatmap", user="postgres", password="1234", host="localhost", port=5433)
Original answer
原答案
Try replacing dbname="gps_heatmap"
with database="gps_heatmap"
as the former is intended for use within a connection string and the latter when keyword arguments are passed to psycopg2.connect()
:
尝试替换dbname="gps_heatmap"
with ,database="gps_heatmap"
因为前者用于在连接字符串中使用,而后者在关键字参数传递给 时使用psycopg2.connect()
:
connection = psycopg2.connect(database="gps_heatmap", user="postgres", host="localhost", password="1234")
Or you could use a connection string:
或者您可以使用连接字符串:
connection = psycopg2.connect("dbname=gps_heatmap user=postgres host=localhost password=1234")
回答by MauryCortes
What worked for me was to open Services (search it in Windows), and then look for the postgres service: postgresql-x64-10 in my case.
对我有用的是打开服务(在 Windows 中搜索它),然后查找 postgres 服务:在我的例子中是 postgresql-x64-10。
- Run the service
- Restart the service
- 运行服务
- 重启服务
After that, the error was gone.
在那之后,错误消失了。
My error originated by installing MySQL after PostgreSQL, I guess there was a change in the ports or something, but this fixed it.
我的错误源于在 PostgreSQL 之后安装 MySQL,我猜端口或其他东西发生了变化,但这修复了它。