Skip to content

How to Fix PostgreSQL Relation Does Not Exist Error

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about How to Fix PostgreSQL Relation Does Not Exist Error. 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, view, or other relation with that name. This usually happens because of a schema search path issue, wrong case, or connecting to the wrong database.

Quick Fix

1. Check the schema search path

PostgreSQL looks for tables in the schemas listed in search_path:

-- Show current search path
SHOW search_path;

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

If the table is in app_schema but the search path only includes public:

-- Wrong — assumes 'public' schema
SELECT * FROM users;

-- Right — schema-qualified
SELECT * FROM app_schema.users;

-- Or update the search path
SET search_path TO app_schema, public;

Make it permanent:

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:

-- Table created with quoted identifier
CREATE TABLE "Users" (id INT);

-- Wrong — looks for "users" (lowercase)
SELECT * FROM Users;

-- Right — match the quoted case
SELECT * FROM "Users";

3. Connect to the right database

Each database has its own set of tables:

-- Show current database
SELECT current_database();

-- List databases
\l

Reconnect to the correct database:

\c mycorrectdb

4. Verify the table was created

The migration that created the table may have failed:

-- List all tables in the current schema
\dt

-- Via SQL
SELECT table_name FROM information_schema.tables
WHERE table_schema = 'public' AND table_type = 'BASE TABLE';

5. Check for views referencing dropped tables

A view that depends on a renamed or dropped table also gives "relation does not exist":

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.
  • Set search_path per user or per database at provisioning time.
  • Use lowercase, unquoted identifiers everywhere.
  • Run migrations in transactions and verify table creation before committing.

Common Mistakes with relation not found

  1. Using return to exit a function early instead of wrapping a pure value in the monad
  2. Mixing let bindings with <- bindings in do notation, producing type errors
  3. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors

These mistakes appear frequently in real-world POSTGRES code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why does the relation exist in one connection but not another?

Each PostgreSQL connection may have a different search_path. The table exists but your session is looking in the wrong schema. Check with SHOW search_path; and qualify the table with its schema.

What is the default search_path?

The default is "$user", public. This means PostgreSQL first looks for a schema matching your username, then the public schema. If neither contains the table, you get "relation does not exist".

How do I list all schemas and their tables?

Run \dn to list schemas, then \dt schema_name.* to list tables in a specific schema. Alternatively query information_schema.tables directly.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro