🔧 Error Fixes

PostgreSQL: Relation Does Not Exist — How to Fix It


ERROR: relation "users" does not exist
LINE 1: SELECT * FROM users;

PostgreSQL can’t find the table you’re querying. It exists (you think), but Postgres disagrees.

Fix 1: Table Doesn’t Exist Yet

Run your migrations first:

# Prisma
npx prisma migrate dev

# Django
python manage.py migrate

# Raw SQL
psql -U postgres -d mydb -f schema.sql

Check what tables exist:

\dt                    -- List all tables
\dt public.*           -- List tables in public schema

Fix 2: Wrong Database

You’re connected to the wrong database.

-- Check current database
SELECT current_database();

-- List all databases
\l

-- Connect to the right one
\c myapp_development

Fix 3: Wrong Schema

The table exists but in a different schema (not public).

-- Check all schemas
\dn

-- Find the table
SELECT table_schema, table_name
FROM information_schema.tables
WHERE table_name = 'users';

-- Query with schema prefix
SELECT * FROM myschema.users;

-- Or set the search path
SET search_path TO myschema, public;

Fix 4: Case Sensitivity

PostgreSQL lowercases unquoted identifiers. If the table was created with quotes, you need quotes to query it.

-- This creates a lowercase table
CREATE TABLE Users (...);     -- Actually creates "users"

-- This creates a case-sensitive table
CREATE TABLE "Users" (...);   -- Creates "Users" (capital U)

-- Query must match
SELECT * FROM users;          -- Works for lowercase
SELECT * FROM "Users";        -- Required for case-sensitive

Fix 5: Transaction Rolled Back

In a transaction, if a previous statement failed, all subsequent statements fail:

BEGIN;
INSERT INTO users ...;        -- This fails
SELECT * FROM users;          -- ERROR: current transaction is aborted
ROLLBACK;                     -- Reset, then try again

Quick Diagnosis

-- Does the table exist anywhere?
SELECT table_schema, table_name
FROM information_schema.tables
WHERE table_name LIKE '%user%';