0

Don't really know how this can be wrong because I'm literally copying along with a coding video but here we are. Says the first arg must be a psycopg2.extension.connection instead of a string?? its not on the documentation nor the guide.

  try:   
        conn = psycopg2.connect(dbname= 'fastapi', host= 'localhost' , port= 5432 , user = 'postgres', password= 'ninjagaiden',connection_factory= RealDictCursor )
        cur = conn.cursor()
        print('database connection succesful')
    except Exception as error :
        print("connection failed")
        print("error explanation:", error)
2
  • Why not to use SQLAlchemy tho, FastAPI shows it as an example db library in their documentation. Commented Feb 3, 2022 at 20:25
  • @sudden_appearance im following a tutorial on building my first rest API Commented Feb 4, 2022 at 10:31

1 Answer 1

1

...connection_factory=RealDictCursor) is the part causing problem Your code should look something like this

try:   
    conn = psycopg2.connect(dbname= 'fastapi', host='localhost', port=5432, user='postgres', password='ninjagaiden')
    cur = conn.cursor(cursor_factory=RealDictCursor)
    print('database connection succesful')
except Exception as error:
    print("connection failed")
    print("error explanation:", error)

P.S. Please try watching after where you put spaces.

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

1 Comment

still fails, it seems the connection factory setting is the reason.

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.