PostgreSQL error 42P01 (Undefined Table) explained: what it means, why it happens, and how to fix it — with copy-paste code examples.
List tables with \dt or information_schema.tables to confirm the name. If that does not apply, schema-qualify the table (schema.table) to avoid search_path ambiguity — the full checklist is below.
SQLSTATE: 42P01
Official name: Undefined Table
System name: undefined_table
Service: PostgreSQL
The referenced table does not exist in the current schema search path.
You may see this error reported as one of the following messages:
relation "users" does not existthere is no unique constraint matching given keys for referenced tableno matching unique or primary key for this column-listA fresh deployment runs SELECT * FROM orders before the migration that creates the table has been applied. PostgreSQL answers SQLSTATE 42P01: relation "orders" does not exist. The same error fires when the table lives in a non-public schema that is not on your search_path, or when you connected to a different database than you thought (common with Docker/CI where the default DB is postgres).
Run these to confirm the cause before applying a fix — each command answers a specific question.
SELECT current_database(), current_schema,
current_setting('search_path');
A wrong database or a search_path without your schema produces 42P01 even though the table exists elsewhere.
SELECT schemaname, tablename FROM pg_tables
WHERE tablename ILIKE '%orders%';
Shows whether the table is missing entirely or just not visible from the current search_path.
Table missing entirely?
├─ migration never ran → apply it (prisma migrate deploy / flyway migrate)
└─ wrong environment/database → fix the connection string
Table exists but in another schema?
├─ reference it qualified: myschema.orders
└─ or add the schema to search_path
Table created moments ago and still 42P01?
└─ in Supabase/PostgREST: NOTIFY pgrst, 'reload schema';
the API layer caches the schema separately
SELECT schemaname, tablename
FROM pg_tables
WHERE tablename ILIKE '%order%';
Search pg_tables by name to confirm spelling, schema, and existence before assuming a missing migration.
Run migrations and check db_table on the model Meta.
class Meta:
db_table = 'orders' # match exactly
Ensure __tablename__ matches and the engine URL targets the right DB.
class Order(Base):
__tablename__ = 'orders'
Confirm the table exists in the project and that the key/role has access.
const { error } = await supabase.from('orders').select('*')
// verify table name + schema in Dashboard
Most often this happens when misspelling the table name or using the wrong case, or when querying a table in the wrong schema due to search_path.
List tables with \dt or information_schema.tables to confirm the name.
This page documents fixes for: django, sqlalchemy, supabase.
Recommendations are editorial — DB Error Reference takes no payment or affiliate fees for tool listings.
This page is based on the official PostgreSQL documentation linked below and adds practical troubleshooting guidance on top.