{"id":21419,"date":"2024-01-02T09:04:24","date_gmt":"2024-01-02T09:04:24","guid":{"rendered":"https:\/\/server.hk\/blog\/21419\/"},"modified":"2024-11-14T10:34:46","modified_gmt":"2024-11-14T02:34:46","slug":"how-to-fix-postgresql-error-code-42p04-duplicate_database","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/21419\/","title":{"rendered":"\u5982\u4f55\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a42P04 &#8211; duplicate_database"},"content":{"rendered":"<h1 id=\"how-to-fix-postgresql-error-code-42p04-duplicate_database-erktdTtLEa\">\u5982\u4f55\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a42P04 &#8211; duplicate_database<\/h1>\n<p>PostgreSQL \u662f\u4e00\u500b\u529f\u80fd\u5f37\u5927\u7684\u958b\u6e90\u95dc\u806f\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u7528\u65bc\u5404\u7a2e\u61c9\u7528\u7a0b\u5e8f\u3002\u7136\u800c\uff0c\u50cf\u6240\u6709\u8edf\u4ef6\u4e00\u6a23\uff0c\u5b83\u4e5f\u53ef\u80fd\u9047\u5230\u9700\u8981\u89e3\u6c7a\u7684\u932f\u8aa4\u3002PostgreSQL \u7528\u6236\u53ef\u80fd\u6703\u9047\u5230\u7684\u4e00\u500b\u5e38\u898b\u932f\u8aa4\u662f\u932f\u8aa4\u4ee3\u78bc 42P04 \u7684 &#8220;duplicate_database&#8221; \u932f\u8aa4\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u63a2\u8a0e\u6b64\u932f\u8aa4\u7684\u542b\u7fa9\uff0c\u4e26\u63d0\u4f9b\u4fee\u5fa9\u6b65\u9a5f\u3002<\/p>\n<h2 id=\"understanding-the-error-erktdTtLEa\">\u7406\u89e3\u932f\u8aa4<\/h2>\n<p>&#8220;duplicate_database&#8221; \u932f\u8aa4\u767c\u751f\u5728\u60a8\u5617\u8a66\u5275\u5efa\u4e00\u500b\u5728 PostgreSQL \u96c6\u7fa4\u4e2d\u5df2\u5b58\u5728\u7684\u6578\u64da\u5eab\u6642\u3002\u6b64\u932f\u8aa4\u662f\u70ba\u4e86\u9632\u6b62\u610f\u5916\u5275\u5efa\u91cd\u8907\u7684\u6578\u64da\u5eab\uff0c\u56e0\u70ba\u9019\u53ef\u80fd\u5c0e\u81f4\u6578\u64da\u5b8c\u6574\u6027\u554f\u984c\u548c\u6df7\u6dc6\u3002<\/p>\n<p>\u7576\u6b64\u932f\u8aa4\u767c\u751f\u6642\uff0c\u60a8\u53ef\u80fd\u6703\u770b\u5230\u985e\u4f3c\u4ee5\u4e0b\u7684\u932f\u8aa4\u6d88\u606f\uff1a<\/p>\n<p><code>ERROR:  duplicate database name \"database_name\" violates uniqueness<\/code><\/p>\n<h2 id=\"fixing-the-error-erktdTtLEa\">\u4fee\u5fa9\u932f\u8aa4<\/h2>\n<p>\u8981\u89e3\u6c7a &#8220;duplicate_database&#8221; \u932f\u8aa4\uff0c\u60a8\u53ef\u4ee5\u9075\u5faa\u4ee5\u4e0b\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-check-existing-databases-erktdTtLEa\">1. \u6aa2\u67e5\u73fe\u6709\u6578\u64da\u5eab<\/h3>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u6aa2\u67e5 PostgreSQL \u96c6\u7fa4\u4e2d\u7684\u73fe\u6709\u6578\u64da\u5eab\u5217\u8868\u3002\u60a8\u53ef\u4ee5\u901a\u904e\u4f7f\u7528 psql \u6216 pgAdmin \u7b49\u5ba2\u6236\u7aef\u5de5\u5177\u9023\u63a5\u5230 PostgreSQL \u4f3a\u670d\u5668\u4f86\u5b8c\u6210\u6b64\u64cd\u4f5c\u3002\u9023\u63a5\u5f8c\uff0c\u904b\u884c\u4ee5\u4e0b SQL \u67e5\u8a62\uff1a<\/p>\n<pre><code>SELECT datname FROM pg_database;<\/code><\/pre>\n<p>\u6b64\u67e5\u8a62\u5c07\u8fd4\u56de\u96c6\u7fa4\u4e2d\u6240\u6709\u6578\u64da\u5eab\u7684\u5217\u8868\u3002\u67e5\u627e\u60a8\u5617\u8a66\u5275\u5efa\u7684\u6578\u64da\u5eab\u540d\u7a31\uff0c\u78ba\u4fdd\u5b83\u4e0d\u5b58\u5728\u3002<\/p>\n<h3 id=\"2-choose-a-unique-database-name-erktdTtLEa\">2. \u9078\u64c7\u4e00\u500b\u552f\u4e00\u7684\u6578\u64da\u5eab\u540d\u7a31<\/h3>\n<p>\u5982\u679c\u60a8\u6253\u7b97\u5275\u5efa\u7684\u6578\u64da\u5eab\u540d\u7a31\u5df2\u5b58\u5728\uff0c\u60a8\u9700\u8981\u70ba\u65b0\u7684\u6578\u64da\u5eab\u9078\u64c7\u4e00\u500b\u4e0d\u540c\u4e14\u552f\u4e00\u7684\u540d\u7a31\u3002\u78ba\u4fdd\u9078\u64c7\u4e00\u500b\u5c1a\u672a\u4f7f\u7528\u7684\u540d\u7a31\uff0c\u4ee5\u907f\u514d\u518d\u6b21\u9047\u5230 &#8220;duplicate_database&#8221; \u932f\u8aa4\u3002<\/p>\n<h3 id=\"3-drop-the-duplicate-database-optional-erktdTtLEa\">3. \u522a\u9664\u91cd\u8907\u7684\u6578\u64da\u5eab\uff08\u53ef\u9078\uff09<\/h3>\n<p>\u5982\u679c\u60a8\u4e0d\u5c0f\u5fc3\u5275\u5efa\u4e86\u91cd\u8907\u7684\u6578\u64da\u5eab\u4e14\u4e0d\u518d\u9700\u8981\u5b83\uff0c\u60a8\u53ef\u4ee5\u9078\u64c7\u522a\u9664\u5b83\u3002\u4f46\u662f\uff0c\u8acb\u8b39\u614e\u64cd\u4f5c\uff0c\u56e0\u70ba\u522a\u9664\u6578\u64da\u5eab\u5c07\u6c38\u4e45\u522a\u9664\u5176\u6240\u6709\u6578\u64da\u3002\u8981\u522a\u9664\u6578\u64da\u5eab\uff0c\u8acb\u904b\u884c\u4ee5\u4e0b SQL \u547d\u4ee4\uff1a<\/p>\n<pre><code>DROP DATABASE database_name;<\/code><\/pre>\n<p>\u5c07 &#8220;database_name&#8221; \u66ff\u63db\u70ba\u60a8\u8981\u522a\u9664\u7684\u91cd\u8907\u6578\u64da\u5eab\u7684\u540d\u7a31\u3002<\/p>\n<h3 id=\"4-create-the-new-database-erktdTtLEa\">4. \u5275\u5efa\u65b0\u7684\u6578\u64da\u5eab<\/h3>\n<p>\u4e00\u65e6\u60a8\u70ba\u65b0\u7684\u6578\u64da\u5eab\u9078\u64c7\u4e86\u4e00\u500b\u552f\u4e00\u7684\u540d\u7a31\uff0c\u60a8\u5c31\u53ef\u4ee5\u7e7c\u7e8c\u5275\u5efa\u5b83\u3002\u4f7f\u7528\u4ee5\u4e0b SQL \u547d\u4ee4\u4f86\u5275\u5efa\u65b0\u7684\u6578\u64da\u5eab\uff1a<\/p>\n<pre><code>CREATE DATABASE new_database_name;<\/code><\/pre>\n<p>\u5c07 &#8220;new_database_name&#8221; \u66ff\u63db\u70ba\u60a8\u5e0c\u671b\u7684\u65b0\u6578\u64da\u5eab\u7684\u540d\u7a31\u3002<\/p>\n<h2 id=\"summary-erktdTtLEa\">\u7e3d\u7d50<\/h2>\n<p>\u7e3d\u4e4b\uff0cPostgreSQL \u4e2d\u7684 &#8220;duplicate_database&#8221; \u932f\u8aa4\u4ee3\u78bc 42P04 \u767c\u751f\u5728\u60a8\u5617\u8a66\u5275\u5efa\u4e00\u500b\u5728\u96c6\u7fa4\u4e2d\u5df2\u5b58\u5728\u7684\u6578\u64da\u5eab\u6642\u3002\u8981\u4fee\u5fa9\u6b64\u932f\u8aa4\uff0c\u60a8\u9700\u8981\u6aa2\u67e5\u73fe\u6709\u6578\u64da\u5eab\uff0c\u70ba\u65b0\u7684\u6578\u64da\u5eab\u9078\u64c7\u4e00\u500b\u552f\u4e00\u540d\u7a31\uff0c\u4e26\u53ef\u9078\u64c7\u522a\u9664\u4efb\u4f55\u91cd\u8907\u7684\u6578\u64da\u5eab\u3002\u901a\u904e\u9075\u5faa\u9019\u4e9b\u6b65\u9a5f\uff0c\u60a8\u53ef\u4ee5\u6210\u529f\u5275\u5efa\u65b0\u7684\u6578\u64da\u5eab\uff0c\u800c\u4e0d\u6703\u9047\u5230 &#8220;duplicate_database&#8221; \u932f\u8aa4\u3002<\/p>\n<p>\u5982\u9700\u53ef\u9760\u548c\u9ad8\u6548\u7684 VPS \u8a17\u7ba1\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e <a href=\"https:\/\/server.hk\">Server.HK<\/a>\u3002\u6191\u85c9\u6211\u5011\u4e00\u6d41\u7684 VPS \u8a17\u7ba1\u670d\u52d9\uff0c\u60a8\u53ef\u4ee5\u78ba\u4fdd PostgreSQL \u6578\u64da\u5eab\u548c\u5176\u4ed6\u61c9\u7528\u7a0b\u5e8f\u7684\u5e73\u7a69\u904b\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4fee\u5fa9 PostgreSQL &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-21419","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=21419"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21419\/revisions"}],"predecessor-version":[{"id":24073,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21419\/revisions\/24073"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=21419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=21419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=21419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}