PostgreSQL 17.12Lab-verified · 2026-11-30

PostgreSQL 42710: already exists (duplicate_object)

You created an object (constraint, trigger, role, type) whose name already exists. Usually a migration or setup script that ran twice.

PostgreSQL 17.12
42710: constraint "chk_orders_amount" for relation "orders" already exists

Jump to a cause

  1. 01ALTER TABLE ... ADD CONSTRAINT run twice
  2. 02CREATE TRIGGER with a name already used on the table
  3. 03CREATE ROLE for a role that already exists
  4. 04CREATE TYPE ... AS ENUM run twice

Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-30 · container image postgres:17

SQLSTATE 42710 is duplicate_object. Tables and indexes give 42P07 instead, but constraints, triggers, roles and types give 42710 when the name is already taken. It most often appears when a migration or init script is run a second time.

Unlike tables, most of these statements have no IF NOT EXISTS, so each case needs its own way to make the script safe to re-run. Four cases verified on PostgreSQL 17.

01ALTER TABLE ... ADD CONSTRAINT run twice

The CHECK constraint chk_orders_amount was already added. Running the same ADD CONSTRAINT again gives 42710 with the constraint and table names.

Setup
Setuprun as lab_a
CREATE TABLE orders (id int PRIMARY KEY, amount numeric);
ALTER TABLE orders ADD CONSTRAINT chk_orders_amount CHECK (amount >= 0);
OUTPUT
CREATE TABLE
ALTER TABLE
Reproducerun as lab_a
ALTER TABLE orders ADD CONSTRAINT chk_orders_amount CHECK (amount >= 0);
✕ ERROR
ERROR:  42710: constraint "chk_orders_amount" for relation "orders" already exists
Checkrun as lab_a
SELECT conname, contype FROM pg_constraint WHERE conrelid = 'orders'::regclass ORDER BY conname;
OUTPUT
      conname      | contype
-------------------+---------
 chk_orders_amount | c
 orders_pkey       | p
(2 rows)

pg_constraint shows chk_orders_amount (c = check) and orders_pkey. ADD CONSTRAINT has no IF NOT EXISTS, so drop and add in one statement: ALTER TABLE orders DROP CONSTRAINT IF EXISTS chk_orders_amount, ADD CONSTRAINT chk_orders_amount CHECK (amount >= 0). It succeeds whether or not the constraint exists.

After the fixrun as lab_a
ALTER TABLE orders DROP CONSTRAINT IF EXISTS chk_orders_amount,
  ADD CONSTRAINT chk_orders_amount CHECK (amount >= 0);
✓ SUCCESS
ALTER TABLE

02CREATE TRIGGER with a name already used on the table

trg_items_touch already exists on items. Creating it again (here to add INSERT to the events) fails.

Setup
Setuprun as lab_a
CREATE TABLE items (id int PRIMARY KEY, name text, updated_at timestamptz);
CREATE FUNCTION touch_updated_at() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN NEW.updated_at := now(); RETURN NEW; END $$;
CREATE TRIGGER trg_items_touch BEFORE UPDATE ON items
  FOR EACH ROW EXECUTE FUNCTION touch_updated_at();
OUTPUT
CREATE TABLE
CREATE FUNCTION
CREATE TRIGGER
Reproducerun as lab_a
CREATE TRIGGER trg_items_touch BEFORE INSERT OR UPDATE ON items
  FOR EACH ROW EXECUTE FUNCTION touch_updated_at();
✕ ERROR
ERROR:  42710: trigger "trg_items_touch" for relation "items" already exists
Checkrun as lab_a
SELECT tgname FROM pg_trigger WHERE tgrelid = 'items'::regclass AND NOT tgisinternal;
OUTPUT
     tgname
-----------------
 trg_items_touch
(1 row)

pg_trigger shows the existing trigger. From PostgreSQL 14 you can write CREATE OR REPLACE TRIGGER, which replaces the definition. After that an INSERT also sets updated_at (touched = t).

After the fixrun as lab_a
CREATE OR REPLACE TRIGGER trg_items_touch BEFORE INSERT OR UPDATE ON items
  FOR EACH ROW EXECUTE FUNCTION touch_updated_at();
INSERT INTO items (id, name) VALUES (1, 'pen');
SELECT id, name, updated_at IS NOT NULL AS touched FROM items;
✓ SUCCESS
CREATE TRIGGER
INSERT 0 1
 id | name | touched
----+------+---------
  1 | pen  | t
(1 row)

03CREATE ROLE for a role that already exists

Roles belong to the whole cluster, not to one database, so a setup script that runs CREATE ROLE app_reader in a second database, or a second time, gets 42710.

Setup
Setuprun as postgres
DROP ROLE IF EXISTS app_reader;
CREATE ROLE app_reader;
OUTPUT
NOTICE:  00000: role "app_reader" does not exist, skipping
DROP ROLE
CREATE ROLE
Reproducerun as postgres
CREATE ROLE app_reader;
✕ ERROR
ERROR:  42710: role "app_reader" already exists
Checkrun as postgres
SELECT rolname FROM pg_roles WHERE rolname = 'app_reader';
OUTPUT
  rolname
------------
 app_reader
(1 row)

CREATE ROLE has no IF NOT EXISTS. Check pg_roles in a DO block: IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'app_reader') THEN CREATE ROLE app_reader; END IF;. The block finishes with DO and no error.

After the fixrun as postgres
DO $$
BEGIN
  IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'app_reader') THEN
    CREATE ROLE app_reader;
  END IF;
END $$;
✓ SUCCESS
DO

04CREATE TYPE ... AS ENUM run twice

order_status already exists with the values {new,paid}. CREATE TYPE has no IF NOT EXISTS either.

Setup
Setuprun as lab_a
CREATE TYPE order_status AS ENUM ('new', 'paid');
OUTPUT
CREATE TYPE
Reproducerun as lab_a
CREATE TYPE order_status AS ENUM ('new', 'paid');
✕ ERROR
ERROR:  42710: type "order_status" already exists
Checkrun as lab_a
SELECT enum_range(NULL::order_status);
OUTPUT
 enum_range
------------
 {new,paid}
(1 row)

Catch the error by name inside a DO block: EXCEPTION WHEN duplicate_object THEN RAISE NOTICE .... The output shows the NOTICE and DO, and the script continues.

After the fixrun as lab_a
DO $$
BEGIN
  CREATE TYPE order_status AS ENUM ('new', 'paid');
EXCEPTION WHEN duplicate_object THEN
  RAISE NOTICE 'order_status already exists, skipping';
END $$;
✓ SUCCESS
NOTICE:  00000: order_status already exists, skipping
DO

FAQ

What is the difference between 42710 and 42P07?

42P07 is a duplicate table, view, sequence or index (relations). 42710 is other objects such as constraints, triggers, roles and types.

Is there ADD CONSTRAINT IF NOT EXISTS in PostgreSQL?

No. Use DROP CONSTRAINT IF EXISTS name, ADD CONSTRAINT name ... in one ALTER TABLE, or check pg_constraint first.

How do I create a role only if it does not exist?

Use a DO block that checks pg_roles before CREATE ROLE.

How do I create an enum type only if it does not exist?

Wrap CREATE TYPE in a DO block with EXCEPTION WHEN duplicate_object THEN and do nothing or raise a notice.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12