【问题标题】:Unable to connect to postgres database with psycopg2无法使用 psycopg2 连接到 postgres 数据库
【发布时间】:2019-12-14 05:41:27
【问题描述】:

我正在尝试连接到 psql 服务器,但不幸的是总是收到此错误。 (windows、vs 代码、python 3.7.4、psycopg2、postgresql 11)

代码部分:

import psycopg2
import json
data = json.load(open("data.json"))


conn = psycopg2.connect(database = "dictfly", user = "postgres", password = "postgres", host = "127.0.0.1", port = "5432")
print("Opened database successfully")
cur = conn.cursor()
for pair in data:
    cur.execute("ISERT INTO dict_tables VALUES (pair, data[pair])")

conn.commit()
print("Records created successfully")
conn.close()

终端

PS C:\mysite\dict> python jsonTOpgsql.py
Traceback (most recent call last):
  File "jsonTOpgsql.py", line 6, in <module>
    conn = psycopg2.connect(database = "dictfly", user = "postgres", password = "postgres0208", host = "127.0.0.1", port = "5432")
  File "C:\Users\uafir\AppData\Local\Programs\Python\Python37-32\lib\site-packages\psycopg2\__init__.py", line 126, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError
PS C:\mysite\dict> 

提前谢谢你!

【问题讨论】:

    标签: python sql postgresql psycopg2


    【解决方案1】:

    我认为“数据库”不是 psycopg2.connect() 的有效关键字参数。尝试使用关键字“dbname”:

    conn = psycopg2.connect(dbname="dictfly", user = "postgres", password = "postgres", host = "127.0.0.1", port = "5432")
    

    来源:psycopg2 documentation

    【讨论】:

    • 试过了,两者都给出相同的输出。
    • 你看过this的帖子吗?
    猜你喜欢
    • 2016-07-31
    • 1970-01-01
    • 2020-10-09
    • 2019-05-05
    • 2011-07-18
    • 1970-01-01
    • 1970-01-01
    • 2017-11-12
    • 2019-11-17
    相关资源
    最近更新 更多