Incident brief
Duplicate object
A CREATE ROLE named a role that already exists. PostgreSQL refused to create a second role under the same name.
In 10 seconds
- What
- Duplicate object
- What triggers it
- CREATE ROLE with a name that already exists.
- The fix
- Wrap the CREATE ROLE in a DO block that catches the duplicate_object exception, for idempotent setup scripts.
- Proof
- Reproduced on PostgreSQL 16.14 → Creating a role with a name that already existed was rejected with SQLSTATE 42710. Exactly one role with that name still exists afterward.
The fix
What to do right now
The immediate, application-level response to this error.
- Wrap the CREATE ROLE in a DO block that catches the duplicate_object exception, for idempotent setup scripts.
- Otherwise, check pg_roles first and only create the role if it's missing.
- Don't widen the exception handler to WHEN OTHERS — see the counterexample for what that silently hides.
DO $$ BEGIN
CREATE ROLE reporting_ro;
EXCEPTION WHEN duplicate_object THEN
NULL;
END $$;Diagnose
See it live on the server
Run these against the affected instance to confirm the diagnosis before you act.
Standard triage — not specific to this error
These are canonical PostgreSQL system-catalog queries, shown as SQL to run. No sample output is attached because this is general triage, not a captured lab transcript.This SQLSTATE does not have an error-specific live snapshot yet. These are the canonical system-catalog queries you run against the affected server to see the problem in real time — standard triage, not a reproduced transcript.
What is running right now
Active backends, how long each has been running, and what it is waiting on.
SELECT pid,
state,
wait_event_type,
wait_event,
now() - query_start AS running_for,
left(query, 80) AS query
FROM pg_stat_activity
WHERE state <> 'idle'
AND pid <> pg_backend_pid()
ORDER BY running_for DESC NULLS LAST;Who is blocking whom
Turn raw blocking PIDs into the actual queries on both sides of the wait.
SELECT blocked.pid AS blocked_pid,
blocked.query AS blocked_query,
blocking.pid AS blocking_pid,
blocking.query AS blocking_query
FROM pg_stat_activity AS blocked
JOIN LATERAL unnest(pg_blocking_pids(blocked.pid)) AS b(pid) ON true
JOIN pg_stat_activity AS blocking ON blocking.pid = b.pid
WHERE cardinality(pg_blocking_pids(blocked.pid)) > 0;Locks that are still waiting
Every lock a backend has requested but not yet been granted.
SELECT l.pid,
l.locktype,
l.mode,
l.granted,
COALESCE(c.relname, l.transactionid::text) AS object
FROM pg_locks l
LEFT JOIN pg_class c ON c.oid = l.relation
WHERE NOT l.granted
ORDER BY l.pid;Why it happens
What PostgreSQL is telling you
The mechanism behind the error, grounded in the official manual — not paraphrased.
PostgreSQL 16 Documentation — CREATE ROLE, CREATEROLE clause
These clauses determine whether a role will be permitted to create, alter, drop, comment on, and change the security label for other roles.Read the full section on postgresql.org →
The duplicate CREATE ROLE
PostgreSQL found an existing role with the same name and refused to create a second one — role names must be unique across the whole cluster.Confirming exactly one role exists
Exactly one reporting_ro exists — the rejected second CREATE ROLE had no effect.Reproduce & verify
A real, single-session PostgreSQL reproduction
A literal transcript of SQL run against a live PostgreSQL instance in an isolated lab — the commands below are exactly what was executed.
- 1CREATE ROLE with a name that already exists.
- 2PostgreSQL refuses to create a duplicate role.
- 3SQLSTATE 42710 is reported, naming the exact role.
One client: create a role, then try to create another role with the same name.
-- No table needed: this reproduction is about roles, not table schema.
SELECT 'no schema required' AS setup_note;DROP ROLE IF EXISTS reporting_ro;
CREATE ROLE reporting_ro;
CREATE ROLE reporting_ro;SELECT count(*) FROM pg_roles WHERE rolname = 'reporting_ro';What PostgreSQL actually returned
setup_note
--------------------
no schema required
(1 row)NOTICE: role "reporting_ro" does not exist, skipping
DROP ROLE
CREATE ROLE
ERROR: role "reporting_ro" already exists count
-------
1
(1 row)The DO-block fix was tested by running it twice in a row against the same already-existing role, to prove it's genuinely idempotent, not just error-free once.
Run the duplicate_object-catching DO block twice against a role that already exists.
Without this
Above: a bare second CREATE ROLE is rejected.
With this, tested
Below: the same CREATE ROLE, wrapped to catch duplicate_object, succeeds silently both times.
What Pro unlocks here
- The exact prevention SQL — copy-paste ready
- Raw psql output captured from the Docker lab
- A senior-DBA action list to take it further
- Live monitoring queries to catch it in production
- The deeper audit: fix-that-fails counterexample, GUC before/after, server-log evidence
Related & next steps
Follow the thread
Everything this error touches — jump straight to the sibling error, term, runbook, or parameter.
Related errors
Verification
- Last verified
- 2026-07-16 (Docker lab, PostgreSQL 16.14)
- Reviewed by
- Verified against PostgreSQL 16.14 in an isolated lab environment
- Audit status
- reviewed