scribase
SQLSTATE 42P07duplicate_tableHTTP 400

Postgres error 42P07: duplicate table

relation "x" already exists

A CREATE statement names a table, index, view or sequence that is already there.

Common causes

  • Running the same migration twice.
  • An index name reused across tables (index names are unique per schema, not per table).

How to fix it

  • Use create table if not exists / create index if not exists in idempotent scripts.
  • Track applied migrations with a migrations table or tool, so each runs once.

Through a REST API

PostgREST, the REST layer behind supabase-js and Scribase, answers HTTP 400 for this error and returns the SQLSTATE in the code field of the JSON error body.

Questions

What does Postgres error 42P07 mean?

42P07 is duplicate_table in class 42 (Syntax Error or Access Rule Violation). A CREATE statement names a table, index, view or sequence that is already there.

How do I fix 42P07?

Use create table if not exists / create index if not exists in idempotent scripts. Track applied migrations with a migrations table or tool, so each runs once.

What HTTP status does a REST API return for 42P07?

PostgREST, the REST layer behind supabase-js, answers 400.

Other class 42 errors

  • 42501 permission denied for table x / new row violates row-level security policy for table "x"
  • 42601 syntax error at or near "x"
  • 42P01 relation "x" does not exist
  • 42P05 prepared statement "s0" already exists
  • 42703 column "x" does not exist
  • 42702 column reference "x" is ambiguous