[Fixed]-Psycopg2 OperationalError: cursor does not exist

5👍

From the psycopg2 documentation:

“Named cursors are usually created WITHOUT HOLD, meaning they live only as long as the current transaction. Trying to fetch from a named cursor after a commit() or to create a named cursor when the connection transaction isolation level is set to AUTOCOMMIT will result in an exception.”

Which is to say that these cursors do not need to be explicitly closed.

http://initd.org/psycopg/docs/usage.html#server-side-cursors

20👍

I made a really simple and silly mistake of forgetting to run ./manage.py makemigrations and ./manage.py migrate before running ./manage.py test which caused this error.

(I’m aware this doesn’t answer the original question, but since this is the first result from Google I thought I would contribute. Hopefully that’s okay)

13👍

I had similar problem and found the solution. Just disable server side cursors like described here: https://docs.djangoproject.com/en/2.2/ref/settings/#disable-server-side-cursors

        'default': {
            ...
            'USER': DB_USER,
            'PASSWORD': DB_PASSWORD,
            'NAME': DB_NAME,
            'DISABLE_SERVER_SIDE_CURSORS': True,
            ...
        },

12👍

I’ve had this problems when playing around with my models and launching the test with Pytest.

What resolved the problem for me was to reset the database of my test unit. I used –create-db like so:

pytest backend/test_projects/partners/test_actions.py --create-db

Leave a comment