wrong object type
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.
- 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
ALTER TABLE applied to a view.
ALTER TABLE my_view ADD COLUMN new_col INT; -- my_view is a VIEW, not a TABLE
expected output
ERROR: "my_view" is not a table
Fix 1
Use the correct command for the object type
WHEN 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 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.
Class 42 — Syntax Error or Access Rule Violation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev