SQLSTATE 34000 ERROR Class 34: Invalid Cursor Name

invalid_cursor_name cursor “…” does not exist — 34000

PostgreSQL error "cursor "…" does not exist" (SQLSTATE 34000): what it means, common causes, and how to fix it.

PG 12, 13, 14, 15, 16, 17, 18 Official docs
Last reviewed May 2025 Grounded in source

Diagnostic Queries

Symptoms

A FETCH, MOVE, or CLOSE referenced a cursor name that is not defined in the session. PostgreSQL raises SQLSTATE 34000 (invalid_cursor_name).

  • The named cursor was never declared or was already closed.
  • Common after a transaction ended and closed the cursor.
  • Non-holdable cursors close at commit.

What the server log shows

ERROR:  cursor "mycursor" does not exist

Why PostgreSQL raises this — what the manual says

the FETCH reference (Description):

“FETCH retrieves rows using a previously-created cursor.”

Cursors live within a session/transaction. Referencing a name that was never declared, was already closed, or expired when its transaction ended cannot be resolved, so PostgreSQL reports 34000.

Common causes

  • A typo in the cursor name.
  • The cursor was already closed (or the transaction committed, closing a non-holdable cursor).
  • Declaring the cursor in a different transaction.

How to fix it

  1. Declare the cursor with DECLARE before fetching, and match the name exactly.
  2. Keep the transaction open while using the cursor, or declare it WITH HOLD.
  3. Verify the cursor wasn’t already closed earlier in the session.

Related & next steps

Reference: PostgreSQL 18 — FETCH.

Was this helpful?