PG
PRO
42710ERRORTier 1 — Safe✅ HIGH confidence

duplicate object

Category: Duplicate ObjectVersions: All Postgres versions

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
CREATE ROLE appuser LOGIN PASSWORD 'secret';
CREATE ROLE appuser LOGIN PASSWORD 'secret'; -- triggers 42710
ERROR: role "appuser" already exists

Fix 1: Use IF NOT EXISTS to make creation idempotent

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

fix
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

Why it's wrong: DROP CASCADE removes dependent objects silently; recreating a role loses all its ACL grants.

Sources

Confidence assessment

✅ HIGH confidence

Well-documented. IF NOT EXISTS support varies by object type; most DDL statements added IF NOT EXISTS support in Postgres 9.3–9.5. Verify that the specific object type supports it before relying on it.

See also

📄 Reference pages

CREATE ROLECREATE SCHEMACREATE EXTENSION
⚙️ This error reference was generated with AI assistance and reviewed for accuracy. Examples are provided to illustrate common scenarios and may not cover every case. Always test fixes in a development environment before applying to production. Spotted an error? Suggest a correction →