FluentDBFluentDB
PostgreSQL errors

PostgreSQL: relation "..." already exists

ERRORSQLSTATE 42P07duplicate_table
ERROR: relation "<name>" already exists

You tried to create a table, index, or other relation whose name is already taken. Use IF NOT EXISTS, or make your migrations idempotent so they do not recreate objects.

What this error means

CREATE fails if an object of that name already exists in the schema. It usually means a migration ran twice, or two processes raced to create the same object.

How to fix it

01

The object already exists

Use IF NOT EXISTS to make creation safe to repeat.

sql
CREATE TABLE IF NOT EXISTS users (id bigint PRIMARY KEY);
CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);
02

A migration is not idempotent

Track applied migrations, or guard each with IF NOT EXISTS, so re-running is safe.

How to avoid it next time

  • Make schema changes idempotent.
  • Use a migration tool that records which migrations have run.

Frequently asked questions

Why did my migration fail on the second run?

It tried to create an object that already exists. Add IF NOT EXISTS, or ensure the migration runner does not re-apply completed migrations.

Does IF NOT EXISTS skip silently?

Yes. If the object exists, Postgres issues a notice and moves on instead of erroring.

Stop struggling with SQL

Use FluentDB, the AI database client for macOS.