PostgreSQLRunbook 0/4 done
42P01

PostgreSQL Error 42P01: Undefined table

Error✕ Not retryable · fix the payloadHTTP 404 Not FoundDatabase
TL;DR · 10-second read

The query references a table or view that does not exist in any schema on the current search_path. Run your migrations, or schema-qualify the name and match its exact case.

Identify: does your log match?

What you see · exact log / response
ERROR:  relation "users" does not exist
LINE 1: SELECT * FROM users;
                      ^
  1. High

    Migrations not applied to this database

    The code was deployed before the migration that creates the table ran. Common when CI points at a fresh database or a different branch database.

  2. Medium

    Table lives in another schema

    Postgres resolves unqualified names through search_path, which defaults to "$user", public. A table in schema app is invisible to SELECT * FROM users.

  3. Medium

    Case-sensitive quoted identifier

    A table created as "Users" (quoted) keeps its capital letter. Unquoted users is folded to lowercase and does not match.

Fix

Schema-qualify or fix the search_path

✕ Broken · SQL
-- table was created in schema "app"
SELECT id, email FROM users;
✓ Fixed · SQL
SELECT id, email FROM app.users;

-- or make it the default for this role
ALTER ROLE api_user SET search_path = app, public;

Qualified names do not depend on search_path. Setting search_path on the role applies to every new session for that user.

Verify: reproduce it on purpose

Reproduce locally · psql
$ psql -c "SELECT * FROM missing_table;"

Run it against a dev environment to trigger 42P01 on purpose and test your error handler.

Prevent

  • Run migrations as a release step in CI, not by hand.
  • Avoid quoted mixed-case identifiers; use snake_case.
  • Pin search_path per role instead of per connection string.

FAQ

Why does 42P01 happen only in production but not locally?

Your local database has migrations applied that production does not. Compare SELECT * FROM _prisma_migrations (or your tool's history table) on both.

Does 42P01 fire for views and sequences too?

Yes. Any relation — table, view, materialized view, sequence or index — that cannot be resolved raises undefined_table.

How do I find which schema a table is in?

Run SELECT table_schema FROM information_schema.tables WHERE table_name = 'users'. Then add that schema to search_path or qualify the name.

Related errors

Last verified 2026-10-19 · Source: Official Appendix A error codes (errcodes.txt in the postgres repo) · Official docs ↗ · ~14,800 searches/mo