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
CREATE TABLE orders (id int PRIMARY KEY, amount numeric);
ALTER TABLE orders ADD CONSTRAINT chk_orders_amount CHECK (amount >= 0);CREATE TABLE
ALTER TABLEALTER TABLE orders ADD CONSTRAINT chk_orders_amount CHECK (amount >= 0);ERROR: 42710: constraint "chk_orders_amount" for relation "orders" already existsSELECT conname, contype FROM pg_constraint WHERE conrelid = 'orders'::regclass ORDER BY conname; 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.
ALTER TABLE orders DROP CONSTRAINT IF EXISTS chk_orders_amount,
ADD CONSTRAINT chk_orders_amount CHECK (amount >= 0);ALTER TABLE02CREATE 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
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();CREATE TABLE
CREATE FUNCTION
CREATE TRIGGERCREATE TRIGGER trg_items_touch BEFORE INSERT OR UPDATE ON items
FOR EACH ROW EXECUTE FUNCTION touch_updated_at();ERROR: 42710: trigger "trg_items_touch" for relation "items" already existsSELECT tgname FROM pg_trigger WHERE tgrelid = 'items'::regclass AND NOT tgisinternal; 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).
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;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
DROP ROLE IF EXISTS app_reader;
CREATE ROLE app_reader;NOTICE: 00000: role "app_reader" does not exist, skipping
DROP ROLE
CREATE ROLECREATE ROLE app_reader;ERROR: 42710: role "app_reader" already existsSELECT rolname FROM pg_roles WHERE rolname = 'app_reader'; 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.
DO $$
BEGIN
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'app_reader') THEN
CREATE ROLE app_reader;
END IF;
END $$;DO04CREATE TYPE ... AS ENUM run twice
order_status already exists with the values {new,paid}. CREATE TYPE has no IF NOT EXISTS either.
Setup
CREATE TYPE order_status AS ENUM ('new', 'paid');CREATE TYPECREATE TYPE order_status AS ENUM ('new', 'paid');ERROR: 42710: type "order_status" already existsSELECT enum_range(NULL::order_status); 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.
DO $$
BEGIN
CREATE TYPE order_status AS ENUM ('new', 'paid');
EXCEPTION WHEN duplicate_object THEN
RAISE NOTICE 'order_status already exists, skipping';
END $$;NOTICE: 00000: order_status already exists, skipping
DOFAQ
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