I am trying to connect to psql server but unfortunately always receiving this error. (windows, vs code, python 3.7.4, psycopg2, postgresql 11)
Code part:
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()
Terminal
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>
Thank you in advance!
asked Aug 6, 2019 at 15:59
1 Answer 1
I believe 'database' is not a valid keyword argument for psycopg2.connect(). Try using the keyword 'dbname' instead:
conn = psycopg2.connect(dbname="dictfly", user = "postgres", password = "postgres", host = "127.0.0.1", port = "5432")
Source: psycopg2 documentation
answered Aug 6, 2019 at 16:07
Sign up to request clarification or add additional context in comments.
2 Comments
Viktor Demychev
Tried, both are giving the same output.
Aedan Pettit
Have you looked at this post?
default