Rails: ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "users" does not exist
Rails queried a table that isn't in the connected PostgreSQL database.
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "users" does not existPG::UndefinedTable: ERROR: relation "schema_migrations" does not existActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: relation does not exist)PG::UndefinedTable ERROR: missing FROM-clause entry for tableCould not find table 'users' Meaning
Migrations weren't run for that environment (bin/rails db:migrate), you're connected to the wrong database, or the schema search path excludes the schema. Check RAILS_ENV, DATABASE_URL, and rails db:migrate:status. In tests run db:test:prepare.
Common causes
- Migrations not run in this environment
- Wrong DATABASE_URL/RAILS_ENV
- Test DB not prepared
- Table renamed/dropped
- Multi-schema
search_pathmismatch
⚡ Quick fix
bin/rails db:migrate(ordb:schema:load) for the right env- Check
bin/rails runner 'puts ActiveRecord::Base.connection.current_database' bin/rails db:test:preparefor tests
Detailed fix by platform
Rails
bin/rails db:migrate:status | head && bin/rails runner 'p ActiveRecord::Base.connection.tables'
How to diagnose
- Env — RAILS_ENV?
- DB — Connected database?
- Migrations — Applied?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- ActiveRecord::AssociationTypeMismatch Rails: ActiveRecord::AssociationTypeMismatch: User(#1234) expected, got "42" which is an instance of String(#5678)
- ActiveRecord::ConnectionNotEstablished Rails: ActiveRecord::ConnectionNotEstablished: No connection pool for 'ActiveRecord::Base' found
- ActiveRecord::Deadlocked: PG::TRDeadlockDetected Rails: ActiveRecord::Deadlocked: PG::TRDeadlockDetected: ERROR: deadlock detected
Most viewed in Ruby / Rails
- undefined method for nil Ruby: NoMethodError: undefined method `x' for nil (nil:NilClass)
- ActionController::InvalidAuthenticityToken Rails: ActionController::InvalidAuthenticityToken (Can't verify CSRF token authenticity)
- Bundler could not find compatible versions for gem Bundler: Bundler could not find compatible versions for gem "activesupport": In Gemfile: rails (= 7.1.0) depends on activesupport (= 7.1.0)
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 5 Nov 2026