Fix "ERROR: relation "users" does not exist" (PostgreSQL)
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.
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.
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
- Migrations not run on this database: fresh database, test database, new Docker volume.
- Wrong database:
psql -U postgresconnects by default to the database named after the user (postgres), orDATABASE_URLpoints to another database. - Schema outside
search_path: the table is inapp.userswhilesearch_pathonly containspublic. - Capital letters and quotes: the ORM created
"User"(Prisma) or"Users"(Sequelize), and the query writes the name without quotes. - Schema privileges: the role lacks
USAGEon the schema, which is then skipped during the lookup.
Step-by-step fix
-
Check where you are connected
These values tell which database, role and schema the query uses. In
psql,\llists databases and\c appconnects to theappdatabase.SELECT current_database(), current_user, current_schema(); SHOW search_path; -
Look for the table in every schema
\dt *.userslooks for the table in every schema of the current database. Theinformation_schemaquery ignores case and reveals a"Users"created with a capital letter; it only shows tables you have privileges on.\dt *.users SELECT table_schema, table_name FROM information_schema.tables WHERE lower(table_name) = 'users'; -
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.php artisan migrate python manage.py migrate npx prisma migrate deploy bin/rails db:migrate -
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, thesearch_pathkey of thepgsqlconnection inconfig/database.phpdoes the same. Also give the roleUSAGEon the schema.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; -
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.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 defaultsearch_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
- Fix "ERROR 1045 (28000): Access denied for user 'root'@'localhost'" (MySQL, MariaDB)
- Fix "ERROR 1062 (23000): Duplicate entry for key" (MySQL, MariaDB, PostgreSQL)
- Fix "Deadlock found when trying to get lock; try restarting transaction" (MySQL, PostgreSQL)
- Fix FATAL: password authentication failed for user "postgres" (PostgreSQL)
- Fix "ERROR 1040: Too many connections" (MySQL) and "too many clients already" (PostgreSQL)
- Fix "ERROR 2006 (HY000): MySQL server has gone away"
- Fix "ERROR 1054 (42S22): Unknown column in 'field list'" (MySQL)