Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

ERROR: relation does not exist for a table that is there

ErrorERROR: relation "users" does not exist

Short answer

The name you typed does not resolve to the table you see. The usual reasons are a table created with a quoted mixed-case name, a table in a schema that is not on search_path, or a connection to a different database. Check with \dt and \conninfo.

  • Applies to PostgreSQL 16, PostgreSQL 17, psql 17
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Confirm which database and user you are connected to, then list tables across all schemas matching the name.

    \conninfo
    \dt *.*users*
  2. 2Match what you see, then jump to its fix

    1. 1\dt shows "Users" with a capital letterThe table was created with a quoted, mixed-case nameFix
    2. 2\dt *.* shows the table under a schema other than publicThe table is in a schema that is not on search_pathFix
    3. 3\dt returns no relations, or different ones than your tool showsYou are connected to a different databaseFix

What does the error look like?

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

Cause 1 of 3

The table was created with a quoted, mixed-case name

1Check

Unquoted names fold to lower case, so Users is read as users. A name made with double quotes keeps its case and must always be quoted.

SELECT schemaname, tablename FROM pg_tables WHERE lower(tablename) = 'users';

2Fix

Quote the name exactly in queries, or rename the table to lower case once.

SELECT * FROM "Users";
-- or, once
ALTER TABLE "Users" RENAME TO users;

3Verify

The unquoted query works.

SELECT count(*) FROM users;

Confirmed by postgresql.org

Did this fix it?

Cause 2 of 3

The table is in a schema that is not on search_path

1Check

An unqualified name is looked up only in the schemas listed in search_path, which defaults to "$user", public.

SHOW search_path;
SELECT schemaname, tablename FROM pg_tables WHERE tablename = 'users';

2Fix

Qualify the name, or add the schema to search_path for the session or the role.

SELECT * FROM app.users;
-- or
SET search_path TO app, public;
-- or persist for a role
ALTER ROLE appuser SET search_path TO app, public;

3Verify

Unqualified queries resolve.

SELECT count(*) FROM users;

Confirmed by postgresql.org

Did this fix it?

Cause 3 of 3

You are connected to a different database

1Check

Tables belong to one database; psql defaults the database name to the user name if you give none.

SELECT current_database(), current_user;
\l

2Fix

Connect to the database that holds the table.

psql -U appuser -d appdb

3Verify

The table is listed.

\dt

Confirmed by postgresql.org

Did this fix it?

How do I stop it coming back?

  • Create tables with unquoted lower_snake_case names.
  • Set search_path per role instead of hoping the session default fits.
  • Put the database name in the connection string, never rely on defaults.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.