簡體   English   中英

使用psycopg2在python中連接到PostgreSQL時遇到問題

[英]Trouble connecting to PostgreSQL in python with psycopg2

我通過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問題?

如果將port = '5433'添加到p.connect線怎么辦?

import psycopg2 as p

con = p.connect("dbname ='dvdrental' user = 'myusername' host ='localhost' password ='somepassword' port='5433'")
cur = con.cursor()
cur.execute("select * from title")
rows = cur.fetchall()

使用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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM