PostgreSQL Error 42809: Wrong Object Type Fix
In this article
PostgreSQL error 42809 (wrong_object_type) means you called a command, function, or operator with an object of the wrong kind — for example, running a table command on a view, or passing a sequence where a table is expected. The fix is to identify the object named in the error, confirm its actual type from the system catalogs, and then use the statement that matches that type (or convert the object to the type the statement requires).
What You'll Need
- Access to the database with a role that can read the system catalogs (pg_class, pg_proc, pg_type) — most roles can, but some locked-down environments restrict it.
- A SQL client such as psql, pgAdmin, DBeaver, or any tool that lets you run queries and see the full error text.
- The exact error message, which includes the object name and often the expected type. Copy it rather than paraphrasing.
Step-by-Step
- Read the full error text. PostgreSQL prints something like
ERROR: "my_object" is not a tableorERROR: wrong object typefollowed by a hint. The object name and the expected type are the two clues you need. If your client truncates the message, run the statement in psql so you see the complete output. - Find out what the object actually is. Query the catalog for its kind. For relations (tables, views, materialized views, sequences, indexes, foreign tables), run:
SELECT relname, relkind FROM pg_class WHERE relname = 'my_object';
Therelkindcolumn tells you the type:r= ordinary table,v= view,m= materialized view,S= sequence,i= index,f= foreign table,p= partitioned table. For functions or procedures, checkpg_proc; for types, checkpg_type. - Match the statement to the object type. Common mismatches and the correct statement:
- Running
ALTER TABLEorTRUNCATEon a view: useALTER VIEWor drop and recreate the view; views cannot be truncated. - Running
ALTER TABLEon a sequence: useALTER SEQUENCE. - Calling a table function with
SELECT * FROM my_func()whenmy_funcis a scalar function: useSELECT my_func()instead. - Passing a view where a table is required by a trigger or rule: triggers can only be defined on tables, so attach the trigger to the underlying table.
- Using
DROP TABLEon a materialized view: useDROP MATERIALIZED VIEW.
- Running
- If the object is the wrong type but you need the operation, convert it. For example, to turn a view into a table, create a new table with
CREATE TABLE new_table AS SELECT * FROM my_view;, then drop the view and rename. To turn a sequence into a table is rarely sensible; instead create the table you need and migrate the data. - Check for name collisions. If two objects with the same name exist in different schemas, your search_path may resolve to the wrong one. Qualify the name explicitly:
myschema.my_object. Confirm withSELECT n.nspname, c.relname, c.relkind FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace WHERE c.relname = 'my_object'; - Re-run the corrected statement. If it succeeds, the error is resolved. If a different 42809 appears, repeat from step 1 — a single script can contain several mismatches.
- If the error comes from application code or an ORM, fix the generated SQL. ORMs sometimes emit
ALTER TABLEfor views or treat sequences as tables during migrations. Check the migration file or query builder output, and override the generated statement with the correct object-specific command.
Common Mistakes
- Assuming the object is a table because the name looks like one. Views, materialized views, and foreign tables all appear in
pg_classalongside real tables; onlyrelkind = 'r'or'p'means a table you canALTER TABLEorTRUNCATE. - Ignoring the schema. A table named
ordersinpublicand a view namedordersin another schema can both exist; the unqualified name picks whichever schema is first insearch_path. - Using
DROP TABLEon a view or sequence. PostgreSQL will refuse with 42809; use the matchingDROP VIEW,DROP MATERIALIZED VIEW, orDROP SEQUENCE. - Treating a scalar function as a set-returning function. Calling it in the FROM clause produces a wrong-object-type error; call it in the SELECT list instead.
- Editing a view with
ALTER TABLE ... ADD COLUMN. Views do not accept column additions that way; recreate the view with the new column in its definition.
Tips
- Use
\d+ object_namein psql to see the object type and its definition at a glance. - Keep the
relkindmapping handy: r = table, v = view, m = materialized view, S = sequence, i = index, f = foreign table, p = partitioned table. - When writing migrations, guard statements with a check on
relkindso the same script works whether the object is a table or a view. - Qualify object names with their schema in scripts that run against multiple environments; it prevents the wrong object from being picked up.
Troubleshooting
- The error names an object you cannot find. It may live in another schema or be a temporary object. Search all schemas with the catalog query from step 5, and check
pg_tempif temporary tables are in use. - You fixed the statement but a different 42809 appears. Work through the script one statement at a time; each mismatch is independent.
- The error comes from a trigger or rule. Triggers can only be attached to tables, not views (unless the view is updatable through an INSTEAD OF trigger on the underlying table). Move the trigger to the base table.
- The error appears only in production. Compare the object types between environments using the catalog query; a view in one environment may be a table in another.
- You are unsure whether an object can be altered at all. Check the PostgreSQL documentation for the specific command; if the object type is not listed as supported, it cannot be altered that way and you must recreate it.
Frequently Asked Questions
What does error 42809 mean in PostgreSQL?
It is the wrong_object_type error: the statement was given an object of a kind it does not accept, such as a view where a table is required.
How do I find out whether an object is a table or a view?
Query pg_class and read relkind: r or p means a table, v a view, m a materialized view, S a sequence.
Can I run TRUNCATE on a view?
No. TRUNCATE only works on tables. To clear data behind a view, truncate the underlying table or delete rows through the view if it is updatable.
Why does ALTER TABLE fail on my sequence?
Sequences are not tables. Use ALTER SEQUENCE to change increment, start value, or ownership.
Does schema search_path cause 42809?
It can. If an unqualified name resolves to an object of the wrong type in an earlier schema, the statement fails. Qualify the name with the correct schema.
Can I convert a view into a table?
Not in place. Create a new table from the view's query with CREATE TABLE ... AS SELECT, then drop the view and rename the table.