PG
PRO
42809ERRORTier 2 — Caution✅ HIGH confidence

wrong object type

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

What this means

SQLSTATE 42809 is raised when a command is applied to an object of the wrong type — for example, using ALTER TABLE on a view, or GRANT TABLE privileges on a sequence.

Why it happens

  1. 1Using a DDL or privilege command that applies to one object type on an object of a different type
  2. 2GRANT, ALTER, or COMMENT applied to the wrong kind of database object

How to reproduce

ALTER TABLE applied to a view.

trigger — this will ERROR
ALTER TABLE my_view ADD COLUMN new_col INT;
-- my_view is a VIEW, not a TABLE
ERROR: "my_view" is not a table

Fix 1: Use the correct command for the object type

When the command is mismatched with the object type.

fix
-- For views, use ALTER VIEW or recreate:
CREATE OR REPLACE VIEW my_view AS SELECT ...;

Why this works

Check the object type with \d in psql or query pg_class, then use the appropriate DDL command for that object type.

Fix 2: Verify the object type before issuing DDL commands

When deploying schema migrations.

fix
SELECT relkind FROM pg_class WHERE relname = 'my_view';
-- r = table, v = view, S = sequence, etc.

Why this works

pg_class.relkind identifies the object type. Use this to guard migrations against applying commands to wrong object types.

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 object type mismatches. Stable across all versions.

See also

📄 Reference pages

pg_classALTER TABLEALTER VIEW
⚙️ 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 →