我无法通过使用psycopg2的python脚本连接到Postgresql服务器。我想通过本地主机本地连接。我可以使用以下参数在pgAdmin4中创建一个新服务器:
dbname=my-database
username=postgres
password=1234
host=localhost
port=5423
这是不起作用的python代码:
import psycopg2 as psy
def create_table():
conn = psy.connect(dbname="testdatabase", user="postgres", password="1234", host="localhost", port="5423")
cur = conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS store(item TEXT, quantity INTEGER, price REAL)")
conn.commit()
conn.close()
def insert(item, quantity, price):
conn = psy.connect(dbname="testdatabase", user="postgres", password="1234", host="localhost", port="5423")
cur = conn.cursor()
cur.execute("INSERT INTO store VALUES(%s, %s, %s)", (item, quantity, price))
conn.commit()
conn.close()
def view():
conn = psy.connect("dbname='testdatabase' user='postgres' password='1234' host='localhost' port='5423'")
cur = conn.cursor()
cur.execute("SELECT * FROM store")
rows = cur.fetchall()
conn.close()
return rows
def delete(item):
conn = psy.connect("dbname='testdatabase' user='postgres' password='1234' host='localhost' port='5423'")
cur = conn.cursor()
cur.execute("DELETE FROM store WHERE item=(?)", (item,)) #need comma after when one item
conn.commit()
conn.close()
def update(item, price, quantity):
conn = psy.connect("dbname='testdatabase' user='postgres' password='1234' host='localhost' port='5423'")
cur = conn.cursor()
cur.execute("UPDATE store SET quantity=?, price=? WHERE item=?", (quantity, price, item))
conn.commit()
conn.close()
create_table()
insert("Golf stuff", 5, 2.65)
这是我的错误:
Traceback (most recent call last):
File "psycopg.py", line 39, in <module>
create_table()
File "psycopg.py", line 4, in create_table
conn = psy.connect("dbname=testdatabase user=postgres password=1234 host=localhost port=5423")
File "C:\Users\Andy Renz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\psycopg2\__init__.py", line 127, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
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 5423?
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 5423?
我浏览了许多论坛,并修改了各种文件。这是他们的状态=>
pg-hba:
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
host replication all 127.0.0.1/32 md5
host replication all ::1/128 md5
PostgreSQL(相关位):
# - Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
port = 5432 # (change requires restart)
max_connections = 100 # (change requires restart)
#superuser_reserved_connections = 3 # (change requires restart)
我的主机文件:
# localhost name resolution is handled within DNS itself.
::1/ localhost
127.0.0.1/ localhost
任何帮助将不胜感激!我很迷路
答案 0 :(得分:0)
超级超级简单。
我写道:
port=5423
应该是:
port=5432
经典错字打发我去解决一个不存在的问题。
感谢所有看过我问题的人!