PostgreSQL Error 42P01: Undefined Table

PostgreSQL error 42P01 (Undefined Table) explained: what it means, why it happens, and how to fix it — with copy-paste code examples.

SQLSTATE 42P01 PostgreSQL Last verified 2026-08-19

Quick Answer

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.

Error Code

SQLSTATE: 42P01
Official name: Undefined Table
System name: undefined_table
Service: PostgreSQL

What does this error mean?

The referenced table does not exist in the current schema search path.

Exact Error Messages

You may see this error reported as one of the following messages:

Common Causes

How to Fix

  1. List tables with \dt or information_schema.tables to confirm the name
  2. Schema-qualify the table (schema.table) to avoid search_path ambiguity
  3. Check that migrations ran and committed in the target database
  4. Verify the connection string points to the right database

Example Scenario

A 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).

Diagnostic Commands

Run these to confirm the cause before applying a fix — each command answers a specific question.

Which database and schema am I actually in? sql
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.

Does the table exist anywhere in this database? sql
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.

Decision Guide

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

Code Examples

Confirm a table exists sql
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.

Framework-Specific Fixes

django

Run migrations and check db_table on the model Meta.

class Meta:
    db_table = 'orders'  # match exactly

sqlalchemy

Ensure __tablename__ matches and the engine URL targets the right DB.

class Order(Base):
    __tablename__ = 'orders'

supabase

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

You Might Also Like

Frequently Asked Questions

Why am I seeing PostgreSQL error 42P01?

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.

How do I fix PostgreSQL error 42P01?

List tables with \dt or information_schema.tables to confirm the name.

Which frameworks have documented fixes for error 42P01?

This page documents fixes for: django, sqlalchemy, supabase.

Official Sources

This page is based on the official PostgreSQL documentation linked below and adds practical troubleshooting guidance on top.