PG::UndefinedTable: relation does not exist Ruby / Rails Origin: Application & API Last updated 5 Nov 2026

Rails: ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "users" does not exist

Rails queried a table that isn't in the connected PostgreSQL database.

Exact message you may see 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'
Seen on: Ruby PostgreSQL

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_path mismatch

⚡ Quick fix

  1. bin/rails db:migrate (or db:schema:load) for the right env
  2. Check bin/rails runner 'puts ActiveRecord::Base.connection.current_database'
  3. bin/rails db:test:prepare for tests

Detailed fix by platform

Rails

  1. bin/rails db:migrate:status | head && bin/rails runner 'p ActiveRecord::Base.connection.tables'

How to diagnose

  1. Env — RAILS_ENV?
  2. DB — Connected database?
  3. 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:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

ActiveRecord::AssociationTypeMismatch — Rails: ActiveRecord::AssociationTypeMismatch: User(#1234) expected, got "42" which is an instance of String(#5678) (Ruby / Rails)ActiveRecord::Associa…ActiveRecord::ConnectionNotEstablished — Rails: ActiveRecord::ConnectionNotEstablished: No connection pool for 'ActiveRecord::Base' found (Ruby / Rails)ActiveRecord::Connect…ActiveRecord::Deadlocked: PG::TRDeadlockDetected — Rails: ActiveRecord::Deadlocked: PG::TRDeadlockDetected: ERROR: deadlock detected (Ruby / Rails)ActiveRecord::Deadloc…ActiveRecord::IrreversibleMigration — Rails: ActiveRecord::IrreversibleMigration — This migration uses remove_column without a type; db:rollback can't reverse it (Ruby / Rails)ActiveRecord::Irrever…ActiveRecord::PendingMigrationError — Rails: ActiveRecord::PendingMigrationError — Migrations are pending. To resolve this issue, run: bin/rails db:migrate (Ruby / Rails)ActiveRecord::Pending…ActiveRecord::ReadOnlyRecord — Rails: ActiveRecord::ReadOnlyRecord: Order is marked as readonly (Ruby / Rails)ActiveRecord::ReadOnl…Mysql2::Error: Unknown column in field list — Rails: ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'users.nickname' in 'field list' (Ruby / Rails)Mysql2::Error: Unknow…PG::ConnectionBad: could not connect to server — Rails: PG::ConnectionBad: could not connect to server: Connection refused. Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432? (Ruby / Rails)PG::ConnectionBad: co…PG::Undef…

Explore the map of this error Full error map →

🧠 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.