1

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!

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Tried, both are giving the same output.
Have you looked at this post?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.