SQLSTATE 42P01 ERROR Class 42: Syntax Error or Access Rule Violation

undefined_table Undefined Table — SQLSTATE 42P01

The table or view does not exist, or is not visible on the search_path.

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

Symptoms

The table or view does not exist, or is not visible on the search_path.

  • The error is written to the server log and returned to the client carrying SQLSTATE 42P01.
  • Any driver (libpq, JDBC, psycopg, npgsql, pgx) surfaces this code in its error object so you can branch on it programmatically.
  • PL/pgSQL can trap it by name: EXCEPTION WHEN undefined_table THEN.

Environment

Severity: ERROR  |  PostgreSQL versions: 12, 13, 14, 15, 16, 17

Reproduce with the exact statement and read the full message in the server log (raise log_min_messages / set log_min_error_statement for more context).

Root Cause

relation does not exist — the name is wrong, lives in another schema, or has not been created in this database.

Common causes:

  • Wrong schema or search_path.
  • Case mismatch on the relation name.
  • Migrations not yet run in this database.
  • Querying another session's temporary table.

Diagnostic Queries

Recovery

Steps to resolve 42P01:

  1. Confirm it exists and where: SELECT schemaname, tablename FROM pg_tables WHERE tablename = '...';.
  2. Schema-qualify the name or fix search_path.
  3. Match case and quoting exactly.
  4. Ensure migrations ran against this database.

Reference: PostgreSQL error codes — Class 42 (Syntax Error or Access Rule Violation).

Was this helpful?