relation does not exist
This error occurs when you reference a table, view, or sequence that PostgreSQL cannot find in the current schema.
The relation does not exist error means PostgreSQL can't find the table, view, or other database object you're trying to use.
Understanding the Error
ERROR: relation "users" does not exist
LINE 1: SELECT * FROM users;
^
The object doesn't exist, or exists in a different schema.
Common Causes
1. Table Doesn't Exist
SQL
2. Wrong Schema
SQL
3. Case Sensitivity
SQL
4. Different Database
Connected to wrong database where the table doesn't exist.
5. Table Not Yet Created in Migration
Accessing table before migration has run.
How to Fix It
Solution 1: Check Table Exists
SQL
Solution 2: Create the Table
SQL
Solution 3: Use Correct Schema
SQL
Solution 4: Check Case Sensitivity
SQL
Solution 5: Verify Database Connection
SQL
Solution 6: Check Migrations
BASH
Finding Tables
SQL
Best Practices
- Use consistent naming (lowercase, snake_case recommended)
- Avoid quoted identifiers unless necessary
- Set search_path appropriately in connection string
- Use IF NOT EXISTS when creating tables
- Check migrations have run in new environments