42P04 — Duplicate database
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.
English manual coverage begins with PostgreSQL 8.1. Source history and runtime observations have their own recorded version boundaries.
Reading PG 18·Select a recorded version
- aliases
- Unknown
- class name
- Syntax Error or Access Rule Violation
- condition name
- duplicate_database
- lang
- en
- sqlstate
- 42P04
Definition PG 18
42P04 — Duplicate database
At a glance {#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 {#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 {#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 {#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 {#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 {#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.
Related {#related}
Sources {#sources}
-
src.errcodes.42P04.18.6—src/backend/utils/errcodes.txtline 379, fixed at724edf9bde9d356724ad384a2e196edc3c9f80f7; SHA-2566e8de346643ba84aa3c9c6a73360acfc7b2dfb89162c06c08ce9bf5bcd5bbcba(source). -
src.call.42P04.465a77b5605c341bc91f6990—src/backend/commands/dbcommands.clines 1393-1395, fixed at724edf9bde9d356724ad384a2e196edc3c9f80f7; SHA-25664b77d2197dce16eb1ad19d8168382459d621a03df0b00be157f7db39ca22acf(source). -
src.call.42P04.b0d1644def83c14a7504fec4—src/backend/commands/dbcommands.clines 1949-1951, fixed at724edf9bde9d356724ad384a2e196edc3c9f80f7; SHA-25664b77d2197dce16eb1ad19d8168382459d621a03df0b00be157f7db39ca22acf(source). -
src.call.42P04.create-tx-guard—src/backend/tcop/utility.clines 769-773, fixed at724edf9bde9d356724ad384a2e196edc3c9f80f7; SHA-2567aae5d07628b6debf8456d1d4ea96f28912232192e56ea25773b4c4b61235a00(source). -
src.calls.REL_18_6.42P04— resolved core call groups; SHA-2569ee8a0e81d8f0825c5c1ae45583439859a26e602bdd4ce2f2a62aa278867ccbf. -
authored evidence— source claims, message roles, and runtime boundary.
Compare versions
SQLSTATE evidence and recorded runtime versions
claims · identity.class-and-condition
{
"claim_id": "identity.class-and-condition",
"limits": "Identity does not enumerate all source branches or client failures.",
"method": "Read fixed REL_18_6 errcodes.txt and locked facts.",
"position": 0,
"runtime": [],
"sources": [
"src.errcodes.42P04.18.6"
],
"statement": "42P04 is the duplicate_database condition in Class 42."
}claims · mechanism.selected-source-path
{
"claim_id": "mechanism.selected-source-path",
"limits": "Representative confirmed core paths only; not exhaustive historical or extension coverage.",
"method": "Read complete resolved REL_18_6 report groups and message roles.",
"position": 1,
"runtime": [],
"sources": [
"src.call.42P04.465a77b5605c341bc91f6990",
"src.call.42P04.b0d1644def83c14a7504fec4",
"src.call.42P04.create-tx-guard"
],
"statement": "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."
}claims · versions.catalogue-boundary
{
"claim_id": "versions.catalogue-boundary",
"limits": "First observed release is a lower bound, not precise behavioral introduction.",
"method": "Read locked catalogue facts and definition snapshots.",
"position": 2,
"runtime": [],
"sources": [
"src.errcodes.42P04.18.6"
],
"statement": "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."
}messages · message.variant-1
{
"limits": "Placeholders remain dynamic source fields; no concrete runtime value is claimed.",
"message_id": "message.variant-1",
"path": "",
"position": 0,
"raw": {
"detail_templates": [],
"hint_templates": [],
"id": "message.variant-1",
"limits": "Placeholders remain dynamic source fields; no concrete runtime value is claimed.",
"primary_template": "database \"%s\" already exists",
"roles": [
{
"role": "errmsg",
"template": "database \"%s\" already exists"
}
],
"severity_source": "explicit ERROR",
"sources": [
"src.call.42P04.465a77b5605c341bc91f6990"
],
"sqlstate": "42P04"
},
"severity": "explicit ERROR",
"sources": [
"src.call.42P04.465a77b5605c341bc91f6990"
],
"templates": [
{
"kind": "primary",
"literal": "database \" \" already exists",
"position": 0,
"role": "",
"template": "database \"%s\" already exists"
}
]
}messages · message.variant-2
{
"limits": "Placeholders remain dynamic source fields; no concrete runtime value is claimed.",
"message_id": "message.variant-2",
"path": "",
"position": 1,
"raw": {
"detail_templates": [],
"hint_templates": [],
"id": "message.variant-2",
"limits": "Placeholders remain dynamic source fields; no concrete runtime value is claimed.",
"primary_template": "database \"%s\" already exists",
"roles": [
{
"role": "errmsg",
"template": "database \"%s\" already exists"
}
],
"severity_source": "explicit ERROR",
"sources": [
"src.call.42P04.b0d1644def83c14a7504fec4"
],
"sqlstate": "42P04"
},
"severity": "explicit ERROR",
"sources": [
"src.call.42P04.b0d1644def83c14a7504fec4"
],
"templates": [
{
"kind": "primary",
"literal": "database \" \" already exists",
"position": 0,
"role": "",
"template": "database \"%s\" already exists"
}
]
}sources · src.errcodes.42P04.18.6
{
"commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
"docs_url": "",
"kind": "upstream_source",
"location": "line 379",
"path": "src/backend/utils/errcodes.txt",
"position": 0,
"sha256": "6e8de346643ba84aa3c9c6a73360acfc7b2dfb89162c06c08ce9bf5bcd5bbcba",
"source_id": "src.errcodes.42P04.18.6",
"tag": "REL_18_6",
"url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/utils/errcodes.txt#L379"
}sources · src.call.42P04.465a77b5605c341bc91f6990
{
"commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
"docs_url": "",
"kind": "upstream_source",
"location": "lines 1393-1395",
"path": "src/backend/commands/dbcommands.c",
"position": 1,
"sha256": "64b77d2197dce16eb1ad19d8168382459d621a03df0b00be157f7db39ca22acf",
"source_id": "src.call.42P04.465a77b5605c341bc91f6990",
"tag": "REL_18_6",
"url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/commands/dbcommands.c#L1393-L1395"
}sources · src.call.42P04.b0d1644def83c14a7504fec4
{
"commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
"docs_url": "",
"kind": "upstream_source",
"location": "lines 1949-1951",
"path": "src/backend/commands/dbcommands.c",
"position": 2,
"sha256": "64b77d2197dce16eb1ad19d8168382459d621a03df0b00be157f7db39ca22acf",
"source_id": "src.call.42P04.b0d1644def83c14a7504fec4",
"tag": "REL_18_6",
"url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/commands/dbcommands.c#L1949-L1951"
}sources · src.call.42P04.create-tx-guard
{
"commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
"docs_url": "",
"kind": "upstream_source",
"location": "lines 769-773",
"path": "src/backend/tcop/utility.c",
"position": 3,
"sha256": "7aae5d07628b6debf8456d1d4ea96f28912232192e56ea25773b4c4b61235a00",
"source_id": "src.call.42P04.create-tx-guard",
"tag": "REL_18_6",
"url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/tcop/utility.c#L769-L773"
}sources · src.calls.REL_18_6.42P04
{
"commit": "",
"docs_url": "",
"kind": "local_artifact",
"location": "",
"path": "raw/calls/REL_18_6.jsonl",
"position": 4,
"sha256": "9ee8a0e81d8f0825c5c1ae45583439859a26e602bdd4ce2f2a62aa278867ccbf",
"source_id": "src.calls.REL_18_6.42P04",
"tag": "",
"url": ""
}Source references
Full definition and evidence JSON
Definition sources
center · PostgreSQL 18 · english-manuals:f1db4505ca7b2febec41002e678784e5ca3205000b2f242e3a94549651fe8102
Text language: en · english-manuals:f1db4505ca7b2febec41002e678784e5ca3205000b2f242e3a94549651fe8102