select open change scope Open full search

PG.CENTER connects PostgreSQL documentation, reference, and ecosystem knowledge. Maintained by Pigsty.

SQLSTATE / CLASS 42 · SYNTAX ERROR OR ACCESS RULE VIOLATION

duplicate_database

SQLSTATE
42P04
Condition name
duplicate_database
Class
Syntax Error or Access Rule Violation
Source macro
ERRCODE_DUPLICATE_DATABASE
Evidence
Source path confirmed
Analysis & operational context

English SQLSTATE atlas: authored explanations and source/runtime evidence are separate from the manual definitions. View source ↗

<h1>42P04 — Duplicate database</h1>

At a glance

42P04 (duplicate_database) means a database-management path found the requested name already in the cluster. It is a cluster-level name check, not a relation collision in the current database, and the CREATE and RENAME callers have different surrounding guards.

Meaning

In createdb, get_database_oid(dbname, true) checks the proposed database name and raises ERROR database "%s" already exists. RenameDatabase performs the same check against newname, after looking up the old database, checking ownership, and checking the CREATEDB privilege. The %s therefore names the requested database in CREATE or the new name in RENAME.

The utility dispatcher calls PreventInTransactionBlock(isTopLevel, "CREATE DATABASE") before it enters createdb. A CREATE DATABASE attempted inside an explicit transaction is therefore a separate transaction-block failure, not evidence that 42P04 was emitted. The RENAME path has its own old-name, owner, privilege, and new-name checks.

Diagnosis

Verify the target cluster and exact name in pg_database, including identifier folding and the possibility that another migration won a race. For CREATE, inspect the proposed name and template/owner prerequisites separately. For RENAME, inspect both old and new names plus ownership and CREATEDB; do not diagnose a missing old database or a privilege failure as 42P04.

Response

If the existing database is intended, make orchestration idempotent and inspect it rather than repeating CREATE DATABASE. Otherwise choose an unused name. Run CREATE DATABASE at top level, outside an explicit transaction block. For RENAME, choose an unused new name only after verifying the old database, ownership, CREATEDB, active sessions, dependencies, and cluster identity. If a RENAME ERROR occurs inside an explicit transaction, issue ROLLBACK before the next command, or ROLLBACK TO a savepoint created before the rename when the outer transaction must be retained. Drop or rename is not a generic repair for every 42P04.

Messages

The selected groups are explicit ERROR with database "%s" already exists: createdb supplies the requested name and RenameDatabase supplies the new name. The dispatcher’s PreventInTransactionBlock guard is source evidence for CREATE DATABASE’s separate transaction restriction, not another 42P04 message.

Versions

From 7.4 through 18.6 and 19beta3; that catalogue range is not a precise introduction claim. The selected CREATE, RENAME, and dispatcher guards are fixed 18.6 source paths; no runtime case was run.

Sources

  • src.errcodes.42P04.18.6src/backend/utils/errcodes.txt line 379, fixed at 724edf9bde9d356724ad384a2e196edc3c9f80f7; SHA-256 6e8de346643ba84aa3c9c6a73360acfc7b2dfb89162c06c08ce9bf5bcd5bbcba (source).

  • src.call.42P04.465a77b5605c341bc91f6990src/backend/commands/dbcommands.c lines 1393-1395, fixed at 724edf9bde9d356724ad384a2e196edc3c9f80f7; SHA-256 64b77d2197dce16eb1ad19d8168382459d621a03df0b00be157f7db39ca22acf (source).

  • src.call.42P04.b0d1644def83c14a7504fec4src/backend/commands/dbcommands.c lines 1949-1951, fixed at 724edf9bde9d356724ad384a2e196edc3c9f80f7; SHA-256 64b77d2197dce16eb1ad19d8168382459d621a03df0b00be157f7db39ca22acf (source).
  • src.call.42P04.create-tx-guardsrc/backend/tcop/utility.c lines 769-773, fixed at 724edf9bde9d356724ad384a2e196edc3c9f80f7; SHA-256 7aae5d07628b6debf8456d1d4ea96f28912232192e56ea25773b4c4b61235a00 (source).
  • src.calls.REL_18_6.42P04 — resolved core call groups; SHA-256 9ee8a0e81d8f0825c5c1ae45583439859a26e602bdd4ce2f2a62aa278867ccbf.
  • authored evidence — source claims, message roles, and runtime boundary.

Source evidence

Evidence belongs to the frozen source and runtime versions listed here. It is not a runtime verification of the selected manual version.

42P04 is the duplicate_database condition in Class 42.

Method: Read fixed REL_18_6 errcodes.txt and locked facts.

Limits: Identity does not enumerate all source branches or client failures.

src.errcodes.42P04.18.6

createdb checks the requested database name and RenameDatabase checks newname after old-database ownership and CREATEDB checks; both report ERROR `database "%s" already exists`. The utility dispatcher separately calls PreventInTransactionBlock for CREATE DATABASE before createdb, so an explicit transaction-block failure is not 42P04.

Method: Read complete resolved REL_18_6 report groups and message roles.

Limits: Representative confirmed core paths only; not exhaustive historical or extension coverage.

src.call.42P04.465a77b5605c341bc91f6990 src.call.42P04.b0d1644def83c14a7504fec4 src.call.42P04.create-tx-guard

From 7.4 through 18.6 and 19beta3; this catalogue range is not a precise introduction claim. The selected CREATE, RENAME, and dispatcher guards are fixed 18.6 source paths and no runtime case was run.

Method: Read locked catalogue facts and definition snapshots.

Limits: First observed release is a lower bound, not precise behavioral introduction.

src.errcodes.42P04.18.6

Message templates

explicit ERROR · message.variant-1

Primary

database "%s" already exists

Placeholders remain dynamic source fields; no concrete runtime value is claimed.

explicit ERROR · message.variant-2

Primary

database "%s" already exists

Placeholders remain dynamic source fields; no concrete runtime value is claimed.

Reproduction & repair cases

No reproduction case is attached to this condition.

Recorded runtime evidence

No runtime observation is attached to this entry.

Definition snapshot: english-manuals:277b595dd676351f4b7af049fcb… · English manual source