How to Fix PostgreSQL 'relation does not exist'
In this tutorial, you'll learn about How to Fix PostgreSQL 'relation does not exist. We cover key concepts, practical examples, and best practices.
The Problem
You run a query and PostgreSQL returns:
ERROR: relation "users" does not exist
LINE 1: SELECT * FROM users;
PostgreSQL cannot find a table or view with that name.
Quick Fix
1. Check the schema search path
PostgreSQL looks for tables in the schemas listed in search_path. If the table is in a schema not in the path, you get this error:
-- Show current search path
SHOW search_path;
-- List all schemas and their tables
SELECT table_schema, table_name FROM information_schema.tables
WHERE table_name = 'users';
If the table is in app_schema but your path only includes public, either qualify the name:
SELECT * FROM app_schema.users;
or update the search path for your session or user:
SET search_path TO app_schema, public;
Make the change permanent for a user:
ALTER USER myuser SET search_path TO app_schema, public;
2. Fix table name case
PostgreSQL folds unquoted identifiers to lowercase. If the table was created with quotes and mixed case, you must match the exact case:
-- Table created with quoted identifier
CREATE TABLE "Users" (id INT);
-- This fails — looks for "users"
SELECT * FROM Users;
-- This works — matches the quoted name
SELECT * FROM "Users";
Best practice: always use lowercase names and avoid quoted identifiers. Check how the table was created:
SELECT table_name FROM information_schema.tables
WHERE table_name ILIKE 'users';
3. Connect to the correct database
Each PostgreSQL database has its own set of tables. You may be connected to the wrong database:
# Show current database
SELECT current_database();
# List all databases
\l
# Reconnect
\c mycorrectdb
If the query worked yesterday but fails today, check which database you connected to.
4. Use the schema-qualified name in application code
In application code, always reference tables with their schema to make the query unambiguous:
SELECT * FROM public.users;
SELECT * FROM analytics.daily_active_users;
This prevents issues when the search path changes in different environments.
5. Verify the table was actually created
If you ran a migration or script that should have created the table, confirm it succeeded:
-- List all tables in the current database
\dt
-- Or via SQL
SELECT table_name FROM information_schema.tables
WHERE table_schema = 'public' AND table_type = 'BASE TABLE';
If the table does not appear, the migration may have failed silently. Check the migration logs and re-run the failed migration.
6. Check for views that reference missing tables
A view that depends on a renamed or dropped table also produces relation does not exist when queried. Find the view definition:
SELECT view_definition FROM information_schema.views
WHERE table_name = 'my_view';
Recreate the view to reference the correct table.
Prevention
- Always use schema-qualified table names in production queries (
schema.table). - Set a default
search_pathper user or per database at provisioning time. - Use lowercase, unquoted identifiers everywhere — it avoids the quoting trap entirely.
- Run database migrations in a transaction and verify table creation before committing.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro