{"id":173971,"date":"2024-11-07T20:28:28","date_gmt":"2024-11-07T12:28:28","guid":{"rendered":"https:\/\/server.hk\/cnblog\/173971\/"},"modified":"2024-11-07T20:28:29","modified_gmt":"2024-11-07T12:28:29","slug":"%e5%a6%82%e4%bd%95%e8%a7%a3%e6%b1%bapg%e6%95%b8%e6%93%9a%e5%ba%ab%e7%b7%a8%e7%a2%bc%e5%95%8f%e9%a1%8c-pg%e6%95%b8%e6%93%9a%e5%ba%ab%e7%b7%a8%e7%a2%bc%e5%95%8f%e9%a1%8c","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/173971\/","title":{"rendered":"\u5982\u4f55\u89e3\u6c7apg\u6578\u64da\u5eab\u7de8\u78bc\u554f\u984c (pg\u6578\u64da\u5eab\u7de8\u78bc\u554f\u984c)"},"content":{"rendered":"<h1 id=\"%e5%a6%82%e4%bd%95%e8%a7%a3%e6%b1%bapg%e6%95%b8%e6%93%9a%e5%ba%ab%e7%b7%a8%e7%a2%bc%e5%95%8f%e9%a1%8c-uPQTosWCao\">\u5982\u4f55\u89e3\u6c7apg\u6578\u64da\u5eab\u7de8\u78bc\u554f\u984c<\/h1>\n<p>\u5728\u4f7f\u7528PostgreSQL\uff08pg\uff09\u6578\u64da\u5eab\u6642\uff0c\u7de8\u78bc\u554f\u984c\u662f\u958b\u767c\u8005\u548c\u6578\u64da\u5eab\u7ba1\u7406\u54e1\u5e38\u5e38\u9762\u81e8\u7684\u6311\u6230\u3002\u9019\u4e9b\u554f\u984c\u53ef\u80fd\u5c0e\u81f4\u6578\u64da\u986f\u793a\u4e0d\u6b63\u78ba\uff0c\u751a\u81f3\u5f71\u97ff\u61c9\u7528\u7a0b\u5e8f\u7684\u6b63\u5e38\u904b\u884c\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u8b58\u5225\u548c\u89e3\u6c7apg\u6578\u64da\u5eab\u4e2d\u7684\u7de8\u78bc\u554f\u984c\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n<h2 id=\"%e7%b7%a8%e7%a2%bc%e5%95%8f%e9%a1%8c%e7%9a%84%e6%a0%b9%e6%ba%90-uPQTosWCao\">\u7de8\u78bc\u554f\u984c\u7684\u6839\u6e90<\/h2>\n<p>\u7de8\u78bc\u554f\u984c\u901a\u5e38\u6e90\u65bc\u4ee5\u4e0b\u5e7e\u500b\u65b9\u9762\uff1a<\/p>\n<ul>\n<li><strong>\u6578\u64da\u8f38\u5165\u6e90\u7684\u7de8\u78bc\u4e0d\u4e00\u81f4\uff1a<\/strong>\u7576\u6578\u64da\u5f9e\u4e0d\u540c\u7684\u4f86\u6e90\uff08\u5982CSV\u6587\u4ef6\u3001API\u7b49\uff09\u5c0e\u5165\u5230\u6578\u64da\u5eab\u6642\uff0c\u9019\u4e9b\u4f86\u6e90\u53ef\u80fd\u4f7f\u7528\u4e0d\u540c\u7684\u7de8\u78bc\u683c\u5f0f\u3002<\/li>\n<li><strong>\u6578\u64da\u5eab\u7684\u7de8\u78bc\u8a2d\u7f6e\u4e0d\u6b63\u78ba\uff1a<\/strong>PostgreSQL\u652f\u6301\u591a\u7a2e\u7de8\u78bc\u683c\u5f0f\uff0c\u5982UTF-8\u3001LATIN1\u7b49\u3002\u5982\u679c\u6578\u64da\u5eab\u7684\u7de8\u78bc\u8a2d\u7f6e\u8207\u5be6\u969b\u5b58\u5132\u7684\u6578\u64da\u4e0d\u5339\u914d\uff0c\u5c07\u6703\u5c0e\u81f4\u986f\u793a\u932f\u8aa4\u3002<\/li>\n<li><strong>\u61c9\u7528\u7a0b\u5e8f\u7684\u7de8\u78bc\u8655\u7406\u4e0d\u7576\uff1a<\/strong>\u5728\u61c9\u7528\u7a0b\u5e8f\u5c64\u9762\uff0c\u5c0d\u6578\u64da\u7684\u7de8\u78bc\u548c\u89e3\u78bc\u8655\u7406\u4e0d\u7576\u4e5f\u6703\u5f15\u767c\u554f\u984c\u3002<\/li>\n<\/ul>\n<h2 id=\"%e6%aa%a2%e6%9f%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e7%b7%a8%e7%a2%bc-uPQTosWCao\">\u6aa2\u67e5\u6578\u64da\u5eab\u7de8\u78bc<\/h2>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u6aa2\u67e5PostgreSQL\u6578\u64da\u5eab\u7684\u7de8\u78bc\u8a2d\u7f6e\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0bSQL\u67e5\u8a62\u4f86\u67e5\u770b\u7576\u524d\u6578\u64da\u5eab\u7684\u7de8\u78bc\uff1a<\/p>\n<pre><code>SELECT pg_encoding_to_char(encoding) FROM pg_database WHERE datname = 'your_database_name';<\/code><\/pre>\n<p>\u5c07 &#8216;your_database_name&#8217; \u66ff\u63db\u70ba\u60a8\u7684\u6578\u64da\u5eab\u540d\u7a31\u3002\u9019\u5c07\u8fd4\u56de\u6578\u64da\u5eab\u7684\u7de8\u78bc\u683c\u5f0f\u3002\u5982\u679c\u767c\u73fe\u7de8\u78bc\u4e0d\u6b63\u78ba\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u8003\u616e\u91cd\u65b0\u5275\u5efa\u6578\u64da\u5eab\u6216\u4f7f\u7528\u8f49\u63db\u5de5\u5177\u3002<\/p>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%b0%8e%e5%85%a5%e6%99%82%e7%9a%84%e7%b7%a8%e7%a2%bc%e8%99%95%e7%90%86-uPQTosWCao\">\u6578\u64da\u5c0e\u5165\u6642\u7684\u7de8\u78bc\u8655\u7406<\/h2>\n<p>\u5728\u5c0e\u5165\u6578\u64da\u6642\uff0c\u78ba\u4fdd\u4f7f\u7528\u6b63\u78ba\u7684\u7de8\u78bc\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528COPY\u547d\u4ee4\u5c0e\u5165CSV\u6587\u4ef6\u6642\uff0c\u53ef\u4ee5\u6307\u5b9a\u7de8\u78bc\uff1a<\/p>\n<pre><code>COPY your_table FROM '\/path\/to\/your\/file.csv' WITH (FORMAT csv, ENCODING 'UTF8');<\/code><\/pre>\n<p>\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u6578\u64da\u4ee5\u6b63\u78ba\u7684\u7de8\u78bc\u683c\u5f0f\u5c0e\u5165\u5230\u6578\u64da\u5eab\u4e2d\u3002<\/p>\n<h2 id=\"%e6%95%b8%e6%93%9a%e8%bd%89%e6%8f%9b-uPQTosWCao\">\u6578\u64da\u8f49\u63db<\/h2>\n<p>\u5982\u679c\u60a8\u5df2\u7d93\u6709\u4e86\u932f\u8aa4\u7de8\u78bc\u7684\u6578\u64da\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u9032\u884c\u8f49\u63db\uff1a<\/p>\n<pre><code>UPDATE your_table SET your_column = convert_from(convert_to(your_column, 'LATIN1'), 'UTF8');<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c07\u6703\u628a\u6307\u5b9a\u5217\u7684\u6578\u64da\u5f9eLATIN1\u7de8\u78bc\u8f49\u63db\u70baUTF-8\u7de8\u78bc\u3002\u6839\u64da\u60a8\u7684\u5be6\u969b\u60c5\u6cc1\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u8abf\u6574\u7de8\u78bc\u683c\u5f0f\u3002<\/p>\n<h2 id=\"%e6%87%89%e7%94%a8%e7%a8%8b%e5%ba%8f%e5%b1%a4%e9%9d%a2%e7%9a%84%e7%b7%a8%e7%a2%bc%e8%99%95%e7%90%86-uPQTosWCao\">\u61c9\u7528\u7a0b\u5e8f\u5c64\u9762\u7684\u7de8\u78bc\u8655\u7406<\/h2>\n<p>\u5728\u61c9\u7528\u7a0b\u5e8f\u4e2d\uff0c\u78ba\u4fdd\u5728\u8207\u6578\u64da\u5eab\u4ea4\u4e92\u6642\u4f7f\u7528\u6b63\u78ba\u7684\u7de8\u78bc\u3002\u4f8b\u5982\uff0c\u5728Java\u4e2d\uff0c\u60a8\u53ef\u4ee5\u5728\u9023\u63a5\u5b57\u7b26\u4e32\u4e2d\u6307\u5b9a\u7de8\u78bc\uff1a<\/p>\n<pre><code>jdbc:postgresql:\/\/localhost:5432\/your_database?charSet=UTF8<\/code><\/pre>\n<p>\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u61c9\u7528\u7a0b\u5e8f\u5728\u8207\u6578\u64da\u5eab\u4ea4\u4e92\u6642\u4f7f\u7528\u6b63\u78ba\u7684\u7de8\u78bc\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-uPQTosWCao\">\u7e3d\u7d50<\/h2>\n<p>\u89e3\u6c7apg\u6578\u64da\u5eab\u7684\u7de8\u78bc\u554f\u984c\u9700\u8981\u5f9e\u591a\u500b\u65b9\u9762\u5165\u624b\uff0c\u5305\u62ec\u6aa2\u67e5\u6578\u64da\u5eab\u7684\u7de8\u78bc\u8a2d\u7f6e\u3001\u6b63\u78ba\u5c0e\u5165\u6578\u64da\u3001\u9032\u884c\u5fc5\u8981\u7684\u6578\u64da\u8f49\u63db\u4ee5\u53ca\u5728\u61c9\u7528\u7a0b\u5e8f\u4e2d\u6b63\u78ba\u8655\u7406\u7de8\u78bc\u3002\u901a\u904e\u9019\u4e9b\u6b65\u9a5f\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u5730\u6e1b\u5c11\u7de8\u78bc\u554f\u984c\u5c0d\u7cfb\u7d71\u7684\u5f71\u97ff\uff0c\u78ba\u4fdd\u6578\u64da\u7684\u6b63\u78ba\u6027\u548c\u4e00\u81f4\u6027\u3002<\/p>\n<p>\u5982\u679c\u60a8\u9700\u8981\u66f4\u591a\u6709\u95dc<em><a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a><\/em>\u548c<em><a href=\"https:\/\/server.hk\">\u96f2\u4f3a\u670d\u5668<\/a><\/em>\u7684\u8cc7\u8a0a\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u8a73\u7d30\u8cc7\u6599\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u89e3\u6c7aPostgreSQL\u6578\u64da\u5eab\u7684\u7de8\u78bc\u554f\u984c\uff0c\u78ba\u4fdd\u6578\u64da\u6b63\u78ba\u986f\u793a\u548c\u5b58\u5132\uff0c\u63d0\u5347\u6578\u64da\u5eab\u6027\u80fd\u548c\u517c\u5bb9\u6027\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-173971","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/173971","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"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=173971"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/173971\/revisions"}],"predecessor-version":[{"id":173972,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/173971\/revisions\/173972"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=173971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=173971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=173971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}