42P07
PostgreSQLERRORCommonDuplicate TableHIGH confidence

relation already exists

What this means

A CREATE TABLE, CREATE VIEW, CREATE MATERIALIZED VIEW, CREATE INDEX, or CREATE SEQUENCE statement attempted to create a relation with a name that already exists in the current schema.

Why it happens
  1. 1Running CREATE TABLE without IF NOT EXISTS in a migration that has already been applied
  2. 2A table with the same name exists in the schema the statement resolves to
  3. 3CREATE INDEX with a name that is already used by another index on any table in the schema
How to reproduce

CREATE TABLE is executed twice for the same table name.

trigger — this will error
trigger — this will error
CREATE TABLE events (id SERIAL PRIMARY KEY, name TEXT);
CREATE TABLE events (id SERIAL PRIMARY KEY, name TEXT); -- triggers 42P07

expected output

ERROR:  relation "events" already exists

Fix

Use IF NOT EXISTS

WHEN In idempotent migration scripts.

Use IF NOT EXISTS
CREATE TABLE IF NOT EXISTS events (
  id   SERIAL PRIMARY KEY,
  name TEXT NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_events_name ON events (name);

Why this works

IF NOT EXISTS checks pg_class for a relation with the given name in the target schema. If found, the statement is suppressed with a NOTICE. The check and the creation are not atomic (a race is possible in concurrent DDL), but this is acceptable for migrations run under a migration lock.

What not to do

Use DROP TABLE IF EXISTS before CREATE TABLE in migrations

Drops all data and dependent objects silently; idempotent CREATE with IF NOT EXISTS is always safer.

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

← All PostgreSQL errors