How to fix Undefined object in PostgreSQL
Error 42704 occurs when a query references a table, column, type, or other database object that does not exist. This commonly happens due to typos, case sensitivity issues, or missing schema qualifiers.
Quick fix
Fixes 42704
-- WRONG: misspelled table name
SELECT * FROM user_table; -- Error: relation "user_table" does not exist
-- CORRECT: proper table name
SELECT * FROM users;- Verify the object name spelling
- Check case sensitivity and quoting
- Verify object exists in the database
- Qualify the object with schema name if needed
-- WRONG: misspelled table nameSELECT * FROM user_table; -- Error: relation "user_table" does not exist-- CORRECT: proper table nameSELECT * FROM users;Undefined object
What this error means
The "Undefined object" error (SQLSTATE 42704) in PostgreSQL indicates that your SQL statement is trying to access a database object that does not exist in the current database or schema. This object could be a table, column, index, constraint, type, function, or any other named database entity. PostgreSQL cannot find the referenced object in the catalog, so the query fails before execution. This is a strict requirement of SQL—all objects must exist before being used.
Typical symptoms
- Query execution fails immediately with SQLSTATE 42704 error
- Error message specifies which object does not exist (e.g., "table does not exist")
- Error appears when creating, altering, or dropping database objects
- Migrations fail at the step referencing a non-existent object
- Works in development but fails in production after deployment
Common causes
- Misspelled object name in the SQL statement
- Case sensitivity mismatch (PostgreSQL folds unquoted identifiers to lowercase)
- Object exists in a different schema and is not qualified with schema name
- Object has not been created yet (migration order issue)
- Object was deleted or dropped without updating dependent code
- Using MySQL syntax or functions that do not exist in PostgreSQL
- Accessing a temporary or session-specific object from a different session
How to fix "Undefined object"
Check the SQL statement for typos in the object name. PostgreSQL error messages typically show which object is missing. For example:
-- WRONG: misspelled table name
SELECT * FROM user_table; -- Error: relation "user_table" does not exist
-- CORRECT: proper table name
SELECT * FROM users;Carefully compare the name in your query with the actual object definition.
PostgreSQL treats unquoted identifiers as lowercase. If you created an object with uppercase letters, you must quote it:
-- Created as uppercase
CREATE TABLE "Users" (id SERIAL);
-- WRONG: fails because it looks for "users" (lowercase)
SELECT * FROM users; -- Error: relation "users" does not exist
-- CORRECT: quote the identifier to preserve case
SELECT * FROM "Users";Best practice: use lowercase names without quoting to avoid these issues.
Use PostgreSQL system tables to check if the object exists:
-- Check if a table exists
SELECT EXISTS (
SELECT 1 FROM information_schema.tables
WHERE table_name = 'users'
);
-- Find which schema contains the object
SELECT table_schema FROM information_schema.tables
WHERE table_name = 'users';
-- List all tables in current schema
\dt
-- List all tables across all schemas
\dt *.*If the object does not exist, you need to create it with a CREATE TABLE, CREATE INDEX, etc. statement.
If the object exists in a different schema, you must include the schema name:
-- Object is in "public" schema
SELECT * FROM public.users;
-- Object is in "archive" schema
SELECT * FROM archive.users;Alternatively, add the schema to your search_path:
SET search_path TO public, archive;
SELECT * FROM users; -- Will find users in public or archiveIf this error occurs during migrations, ensure prerequisite objects are created first:
-- Migration 001: Create base table
CREATE TABLE users (
id SERIAL PRIMARY KEY,
email VARCHAR(255) UNIQUE
);
-- Migration 002: Create dependent table
CREATE TABLE posts (
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(id) -- users must exist first
);Use transactions and IF NOT EXISTS to make migrations idempotent:
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY
);
CREATE TABLE IF NOT EXISTS posts (
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(id)
);Advanced notes
Related error codes: SQLSTATE 42P01 (undefined_table) for missing tables specifically, SQLSTATE 42703 (undefined_column) for missing columns, SQLSTATE 42883 (undefined_function) for missing functions. When using application ORMs or migrations tools, ensure they generate valid PostgreSQL syntax. Some frameworks (Laravel, Django) may need dialect-specific SQL. For distributed systems, confirm all replicas and standby servers have identical object definitions. Use pg_dump --schema-only to verify schema consistency across databases.
Frequently asked questions
- What does "42704" mean?
- The "Undefined object" error (SQLSTATE 42704) in PostgreSQL indicates that your SQL statement is trying to access a database object that does not exist in the current database or schema. This object could be a table, column, index, constraint, type, function, or any other named database entity. PostgreSQL cannot find the referenced object in the catalog, so the query fails before execution.
- How do I fix "42704"?
- Error 42704 occurs when a query references a table, column, type, or other database object that does not exist. This commonly happens due to typos, case sensitivity issues, or missing schema qualifiers.
- What causes "42704"?
- Misspelled object name in the SQL statement Case sensitivity mismatch (PostgreSQL folds unquoted identifiers to lowercase) Object exists in a different schema and is not qualified with schema name
- Is the "42704" error serious?
- It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- 📚PostgreSQL Official Documentation
- 📖PostgreSQL Error Codes(Official Docs)