SQLSTATE 42710 ERROR Class 42: Syntax Error or Access Rule Violation

duplicate_object role “…” already exists — 42710

PostgreSQL error "role "…" already exists" (SQLSTATE 42710): what it means, common causes, and how to fix it.

PG 12, 13, 14, 15, 16, 17, 18 Official docs
Last reviewed May 2025 Grounded in source

Diagnostic Queries

Symptoms

A CREATE ROLE/CREATE USER tried to create a role whose name already exists. PostgreSQL raises SQLSTATE 42710 (duplicate_object).

  • A role with that name already exists.
  • Roles are cluster-wide (shared across databases).
  • Common in idempotent provisioning scripts.

What the server log shows

ERROR:  role "app_user" already exists

Why PostgreSQL raises this — what the manual says

As the CREATE ROLE reference (Description) explains:

A role with that name already exists; role names must be unique across the entire cluster, so choose a different name or use ALTER ROLE to modify the existing one.

Role names are unique cluster-wide. Creating one that already exists collides with the existing role, so PostgreSQL reports 42710.

Common causes

  • Re-running a provisioning script that creates the role.
  • The role was created earlier (roles are cluster-wide).
  • A name clash with a pre-existing role.

How to fix it

  1. Skip creation if it exists: check pg_roles or use DO/conditional logic.
  2. Use CREATE ROLE … only when absent; otherwise ALTER ROLE to adjust.
  3. Choose a unique role name.

Related & next steps

Reference: PostgreSQL 18 — CREATE ROLE.

Was this helpful?