42710
PostgreSQLERRORCommonDuplicate ObjectHIGH confidence

duplicate object

What this means

A CREATE statement attempted to create a database object (role, schema, extension, trigger, etc.) that already exists. Unlike 42P07 which is specific to relations, 42710 covers a wider set of non-relation object types.

Why it happens
  1. 1Running CREATE ROLE, CREATE SCHEMA, or CREATE EXTENSION when the object already exists
  2. 2Migration scripts run multiple times without idempotency checks
  3. 3CREATE TRIGGER with a name that already exists on the same table
  4. 4Installing an extension that was already installed in the database
How to reproduce

CREATE ROLE is run twice for the same role name.

trigger — this will error
trigger — this will error
CREATE ROLE appuser LOGIN PASSWORD 'secret';
CREATE ROLE appuser LOGIN PASSWORD 'secret'; -- triggers 42710

expected output

ERROR:  role "appuser" already exists

Fix

Use IF NOT EXISTS to make creation idempotent

WHEN In migration scripts or setup scripts that may be re-run.

Use IF NOT EXISTS to make creation idempotent
CREATE ROLE IF NOT EXISTS appuser LOGIN PASSWORD 'secret';
CREATE SCHEMA IF NOT EXISTS myapp;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

Why this works

IF NOT EXISTS causes the DDL statement to check the catalog for an existing object with the same name before attempting creation. If found, the statement is a no-op (returns a NOTICE instead of an error). This is a catalog-level check, not a lock-free operation.

What not to do

Drop and recreate the object to avoid the error in production

DROP CASCADE removes dependent objects silently; recreating a role loses all its ACL grants.

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

← All PostgreSQL errors