duplicate object
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.
- 1Running CREATE ROLE, CREATE SCHEMA, or CREATE EXTENSION when the object already exists
- 2Migration scripts run multiple times without idempotency checks
- 3CREATE TRIGGER with a name that already exists on the same table
- 4Installing an extension that was already installed in the database
CREATE ROLE is run twice for the same role name.
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.
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.
✕ 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