PG
PRO
42704ERRORTier 2 — Caution✅ HIGH confidence

undefined object

Category: Syntax Error or Access Rule ViolationVersions: All Postgres versions

What this means

SQLSTATE 42704 is raised when a command references a database object (type, operator, cast, function, extension, etc.) that does not exist. It is distinct from 42P01 (undefined table) and is used for non-table objects.

Why it happens

  1. 1Referencing a user-defined type, operator, cast, text search configuration, or extension that does not exist
  2. 2COMMENT ON or GRANT targeting a non-existent object
  3. 3Using a collation, encoding, or foreign data wrapper name that is not installed

How to reproduce

Referencing a non-existent type.

trigger — this will ERROR
ALTER TABLE orders ADD COLUMN payload custom_type;
-- custom_type does not exist
ERROR: type "custom_type" does not exist

Fix 1: Create the type or install the extension before referencing it

When the object is missing.

fix
CREATE TYPE custom_type AS (field1 TEXT, field2 INT);
ALTER TABLE orders ADD COLUMN payload custom_type;

Why this works

The referenced object must exist before it can be used. Create it or install the required extension first.

Fix 2: Check for the object in the catalogue before using it

When writing migration scripts.

fix
SELECT typname FROM pg_type WHERE typname = 'custom_type';

Why this works

Querying pg_type, pg_operator, pg_cast, etc., confirms that the object exists before referencing it in DDL.

Sources

📚 Official docs: https://www.postgresql.org/docs/current/errcodes-appendix.html

🔧 Source ref: Class 42 — Syntax Error or Access Rule Violation

Confidence assessment

✅ HIGH confidence

Standard SQLSTATE for missing object references. Stable across all versions.

See also

📄 Reference pages

CREATE TYPECREATE EXTENSIONpg_type
⚙️ This error reference was generated with AI assistance and reviewed for accuracy. Examples are provided to illustrate common scenarios and may not cover every case. Always test fixes in a development environment before applying to production. Spotted an error? Suggest a correction →