PostgreSQL Error 42P01: Undefined table
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?
ERROR: relation "users" does not exist
LINE 1: SELECT * FROM users;
^- 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.
- 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.
- 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
-- table was created in schema "app" SELECT id, email FROM users;
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
$ 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