42809ERRORTier 2 — Caution✅ HIGH confidencewrong object type
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
- 1Using a DDL or privilege command that applies to one object type on an object of a different type
- 2GRANT, ALTER, or COMMENT applied to the wrong kind of database object
How to reproduce
ALTER TABLE applied to a view.
ALTER TABLE my_view ADD COLUMN new_col INT;
-- my_view is a VIEW, not a TABLEFix 1: Use the correct command for the object type
When the command is mismatched with the object type.
-- 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.
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