'Error message- postgres db connection- says argument 1 should be psycopg2 object?

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)


Solution 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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sudden_appearance