我在通过 postgreSQL3 9.5 版连接到我的数据库时遇到问题。然而,在运行下面的代码之后:
import psycopg2 as p
con = p.connect("dbname ='dvdrental' user = 'myusername' host ='localhost' password ='somepassword'")
cur = con.cursor()
cur.execute("select * from title")
rows = cur.fetchall()
我收到此错误消息:
psycopg2.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?
关于背景信息,我不小心下载了最新版本的PostgreSQL,它连接到5432部分。我需要它连接到PostgreSQL端口5433。我不知道该怎么做。我该如何解决这个数据库问题?这是 PostgreSQL 问题还是 Python 问题?
最佳答案
使用 netstat
(从 shell)检查 postgres 的监听地址:
plasser@pisbak$ netstat -nl |grep 5432
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN
tcp6 0 0 :::5432 :::* LISTEN
unix 2 [ ACC ] STREAM LISTENING 9002 /tmp/.s.PGSQL.5432
plasser@pisbak$
如果没有任何显示,Postgres 没有在端口 5432 上监听。
关于python - 使用 psycopg2 在 python 中连接到 PostgreSQL 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44933326/