42P04 — 重复数据库
集群中已存在同名数据库。
英文手册覆盖始于 PostgreSQL 8.1. 来源历史与运行验证各自保留独立版本边界。
当前阅读 PG 18·选择有来源记录的版本
- aliases
- 未知
- class name
- Syntax Error or Access Rule Violation
- condition name
- duplicate_database
- lang
- en
- sqlstate
- 42P04
版本定义 PG 18
42P04 — 重复数据库
速览 {#at-a-glance}
42P04(duplicate_database)表示数据库管理路径发现集群中已有同名数据库。这是集群级名称检查,不是当前库内的关系冲突;CREATE 和 RENAME 的外围检查也不同。
含义 {#meaning}
createdb 会用 get_database_oid(dbname, true) 检查目标名称,冲突时报告 ERROR database "%s" already exists。RenameDatabase 在查找旧数据库、检查所有权和 CREATEDB 权限之后,对 newname 做同样检查。因此 %s 在 CREATE 中是请求的数据库名,在 RENAME 中是新名称。
utility 分发器在进入 createdb 前调用 PreventInTransactionBlock(isTopLevel, "CREATE DATABASE")。所以在显式事务中执行 CREATE DATABASE 是另一种事务块错误,不能据此声称发出了 42P04;RENAME 则有独立的旧名、所有权、权限和新名检查。
诊断 {#diagnosis}
确认目标集群和准确名称,检查 pg_database、标识符折叠和迁移竞争。CREATE 另查目标名称、模板和所有者前提;RENAME 同时查旧名、新名、所有权和 CREATEDB。旧数据库不存在或权限不足不应误判为 42P04。
处理 {#response}
已有库就是目标时让编排幂等并检查它;否则选择未占用名称。CREATE DATABASE 要在顶层、显式事务块之外执行。RENAME 只有在确认旧库、所有权、CREATEDB、活动会话、依赖和集群身份后才选择可用新名。如果 RENAME 在显式事务中报错,下一条命令前应执行 ROLLBACK;若必须保留外层事务,则回滚到改名前建立的保存点。删除或改名不是所有 42P04 的通用修复。
消息变体 {#messages}
选定路径均为 ERROR database "%s" already exists:createdb 使用请求名称,RenameDatabase 使用新名称。utility 中的 PreventInTransactionBlock 证明 CREATE DATABASE 有独立事务限制,不是另一条 42P04 报文。
版本 {#versions}
目录范围从 7.4 到 18.6、19beta3,不是精确的功能引入判断。选定的是 18.6 固定 CREATE、RENAME 和分发器事务检查路径,未运行数据库。
相关 {#related}
来源 {#sources}
-
src.errcodes.42P04.18.6—src/backend/utils/errcodes.txtline 379,固定于724edf9bde9d356724ad384a2e196edc3c9f80f7;SHA-2566e8de346643ba84aa3c9c6a73360acfc7b2dfb89162c06c08ce9bf5bcd5bbcba(源码)。 -
src.call.42P04.465a77b5605c341bc91f6990—src/backend/commands/dbcommands.clines 1393-1395,固定于724edf9bde9d356724ad384a2e196edc3c9f80f7;SHA-25664b77d2197dce16eb1ad19d8168382459d621a03df0b00be157f7db39ca22acf(源码)。 -
src.call.42P04.b0d1644def83c14a7504fec4—src/backend/commands/dbcommands.clines 1949-1951,固定于724edf9bde9d356724ad384a2e196edc3c9f80f7;SHA-25664b77d2197dce16eb1ad19d8168382459d621a03df0b00be157f7db39ca22acf(源码)。 -
src.call.42P04.create-tx-guard—src/backend/tcop/utility.clines 769-773,固定于724edf9bde9d356724ad384a2e196edc3c9f80f7;SHA-2567aae5d07628b6debf8456d1d4ea96f28912232192e56ea25773b4c4b61235a00(源码)。 -
src.calls.REL_18_6.42P04— 固定本地核心调用扫描;SHA-2569ee8a0e81d8f0825c5c1ae45583439859a26e602bdd4ce2f2a62aa278867ccbf。 -
作者证据— 记录源码断言、消息角色和运行边界。
比较版本
SQLSTATE 证据与已记录的运行版本
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": ""
}来源引用
定义来源
center · PostgreSQL 18 · english-manuals:f1db4505ca7b2febec41002e678784e5ca3205000b2f242e3a94549651fe8102
正文语言: zh-Hans · english-manuals:f1db4505ca7b2febec41002e678784e5ca3205000b2f242e3a94549651fe8102