42809
PostgreSQLERRORNotableSyntax Error or Access Rule ViolationHIGH confidence

wrong 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
  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
trigger — this will error
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.

Use the correct command for 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.

Verify the object type before issuing DDL commands
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 documentation ↗

Class 42 — Syntax Error or Access Rule Violation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All PostgreSQL errors