Skip to content
FR
  1. LuzineDebug
  2. Databases (SQL)
  3. ERROR: relation "users" does not exist
Databases (SQL) Intermediate ≈ 10 min to fix

Fix "ERROR: relation "users" does not exist" (PostgreSQL)

Error message
ERROR:  relation "users" does not exist
  • SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "users" does not exist
  • psycopg2.errors.UndefinedTable: relation "users" does not exist
  • ERROR: relation "public.users" does not exist
  • ERROR: relation "users_id_seq" does not exist
  • The table `public.User` does not exist in the current database.
In short

PostgreSQL cannot find the table: it was not created in this database (migrations not run), you are connected to another database, it lives in a schema missing from search_path, or it was created with quoted capital letters ("Users"). Check current_database() and SHOW search_path;, look for the table with \dt *.users, then run the migrations or qualify the name.

Be the first to confirm this fix

What this error means

For PostgreSQL, a "relation" is a table, view, materialized view, sequence or index. The error means the name written in the query could not be resolved in the current database. An unqualified name is looked up in each schema of search_path (by default "$user", public), and schemas on which the role lacks the USAGE privilege are skipped silently. PostgreSQL also lowercases every unquoted identifier: Users becomes users, while a table created as "Users" keeps its capital letter and must always be written in quotes. Finally, each database is isolated: a table in the app database is invisible from the postgres database.

Common causes

  1. Migrations not run on this database: fresh database, test database, new Docker volume.
  2. Wrong database: psql -U postgres connects by default to the database named after the user (postgres), or DATABASE_URL points to another database.
  3. Schema outside search_path: the table is in app.users while search_path only contains public.
  4. Capital letters and quotes: the ORM created "User" (Prisma) or "Users" (Sequelize), and the query writes the name without quotes.
  5. Schema privileges: the role lacks USAGE on the schema, which is then skipped during the lookup.

Step-by-step fix

  1. Check where you are connected

    These values tell which database, role and schema the query uses. In psql, \l lists databases and \c app connects to the app database.

    sql
    SELECT current_database(), current_user, current_schema();
    SHOW search_path;
  2. Look for the table in every schema

    \dt *.users looks for the table in every schema of the current database. The information_schema query ignores case and reveals a "Users" created with a capital letter; it only shows tables you have privileges on.

    sql
    \dt *.users
    
    SELECT table_schema, table_name
    FROM information_schema.tables
    WHERE lower(table_name) = 'users';
  3. Run the migrations on the right database

    If the table exists nowhere, create it with the project’s migration tool, targeting the application database. In Docker, run the command inside the application container (docker compose exec app php artisan migrate). For the Laravel test database: php artisan migrate --env=testing.

    bash
    php artisan migrate
    python manage.py migrate
    npx prisma migrate deploy
    bin/rails db:migrate
  4. Set the schema and search_path

    Qualify the name with the schema, or add that schema to the role’s search_path (applies to new sessions). In Laravel, the search_path key of the pgsql connection in config/database.php does the same. Also give the role USAGE on the schema.

    sql
    SELECT * FROM app.users;
    
    ALTER ROLE app_user SET search_path = app, public;
    GRANT USAGE ON SCHEMA app TO app_user;
    GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA app TO app_user;
  5. Handle capital letters and quotes

    A table created with quotes and capital letters must be written exactly the same way, in double quotes. To get rid of it, rename it to lowercase, and update the ORM at the same time (with Prisma, @@map("users") on the model) or the ORM will be the one unable to find the table.

    sql
    SELECT * FROM "User";
    
    ALTER TABLE "User" RENAME TO users;

Did this fix work for you?

Confirm it in one click: it will rise for the next developers stuck on the same error.

Keep it from coming back

  • Name tables and columns in lowercase snake_case, without quotes: you will never have case issues.
  • Run migrations at deploy time and in CI, and always include the database name in DATABASE_URL.
  • If you use a schema other than public, declare it explicitly in the connection configuration rather than relying on the default search_path.

Frequently asked questions

Why does the table show in pgAdmin but not for my application?

The application is connected to another database, with another role or another search_path. Compare current_database(), current_user and SHOW search_path; in both cases.

Why does PostgreSQL lowercase my table name?

It is PostgreSQL’s SQL rule: unquoted identifiers are folded to lowercase. Only names in double quotes keep their exact case.

What if the error is about "users_id_seq"?

The sequence has another name or does not exist (partially restored table, renamed column). Find the real name with SELECT pg_get_serial_sequence('users', 'id'); and use it in your code.

Useful tools for this error

More Databases (SQL) errors

All Databases (SQL) errors →