{"id":200683,"date":"2025-05-06T14:06:52","date_gmt":"2025-05-06T06:06:52","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200683\/"},"modified":"2025-05-06T14:06:52","modified_gmt":"2025-05-06T06:06:52","slug":"%e5%a6%82%e4%bd%95%e5%b0%86%e6%95%b0%e6%8d%ae%e9%ab%98%e6%95%88%e5%af%bc%e5%85%a5-postgresql-%e6%95%b0%e6%8d%ae%e5%ba%93%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200683\/","title":{"rendered":"\u5982\u4f55\u5c06\u6570\u636e\u9ad8\u6548\u5bfc\u5165 PostgreSQL \u6570\u636e\u5e93\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u5c06\u6570\u636e\u9ad8\u6548\u5bfc\u5165 PostgreSQL \u6570\u636e\u5e93\uff1f<\/h1>\n<p>\u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684\u884c\u4e1a\uff0c\u53ea\u6709\u4e0d\u505c\u6b62\u7684\u5b66\u4e60\uff0c\u624d\u4e0d\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u5982\u679c\u4f60\u662f\u6570\u636e\u5e93\u5b66\u4e60\u8005\uff0c\u90a3\u4e48\u672c\u6587\u300a\u5982\u4f55\u5c06\u6570\u636e\u9ad8\u6548\u5bfc\u5165 PostgreSQL \u6570\u636e\u5e93\uff1f\u300b\u5c31\u5f88\u9002\u5408\u4f60\uff01\u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u5305\u62ec##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241111\/17313160186731c932b4eed.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5c06\u6570\u636e\u5b58\u50a8\u5230 postgresql \u6570\u636e\u5e93\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>\u5bf9\u4e8e\u65b0\u624b\u6765\u8bf4\uff0c\u5c06\u590d\u6742\u683c\u5f0f\u7684\u6570\u636e\u5bfc\u5165\u6570\u636e\u5e93\u53ef\u80fd\u4f1a\u8ba9\u4eba\u671b\u800c\u751f\u754f\uff0c\u4f46\u638c\u63e1\u6b63\u786e\u7684\u6280\u672f\u53ef\u4ee5\u7b80\u5316\u8fd9\u4e00\u8fc7\u7a0b\u3002\u672c\u6587\u5c06\u91cd\u70b9\u4ecb\u7ecd\u5982\u4f55\u5c06\u7c7b\u4f3c\u4e8e\u63d0\u4f9b\u7684\u793a\u4f8b\u6570\u636e\u5bfc\u5165 postgresql \u6570\u636e\u5e93\u3002<\/p>\n<p><strong>\u4f7f\u7528 mysql<\/strong><\/p>\n<p>\u4e3a\u4e86\u5728 mysql \u4e2d\u5bfc\u5165\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528 load data infile \u8bed\u53e5\u3002\u8be5\u8bed\u53e5\u5141\u8bb8\u60a8\u4ece\u6587\u672c\u6587\u4ef6\u76f4\u63a5\u5c06\u6570\u636e\u52a0\u8f7d\u5230\u8868\u4e2d\u3002\u8be6\u7ec6\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>load data infile 'file_name.txt' into table table_name\n(column_name1, column_name2, ...)<\/pre>\n<p><strong>\u4f7f\u7528 postgresql<\/strong><\/p>\n<p>postgresql \u63d0\u4f9b\u4e86 copy \u547d\u4ee4\uff0c\u5b83\u4e0e mysql \u7684 load data infile \u7c7b\u4f3c\u3002\u8be5\u547d\u4ee4\u5141\u8bb8\u60a8\u5c06\u6570\u636e\u4ece\u6587\u4ef6\u6216\u6807\u51c6\u8f93\u5165\u4e2d\u590d\u5236\u5230\u8868\u4e2d\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>copy table_name (column_name1, column_name2, ...)\nfrom 'file_name.txt'<\/pre>\n<p>\u6b64\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528 python \u7684 psycopg2 \u5e93\u6765\u4e0e postgresql \u6570\u636e\u5e93\u4ea4\u4e92\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528 psycopg2 \u5c06\u6570\u636e\u63d2\u5165\u5230 info \u8868\u4e2d\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>import psycopg2\n\n# \u5efa\u7acb\u8fde\u63a5\nconn = psycopg2.connect(\n    \"host=localhost\",\n    \"dbname=database_name\",\n    \"user=username\",\n    \"password=password\"\n)\n\n# \u521b\u5efa\u6e38\u6807\ncur = conn.cursor()\n\n# INSERT \u8bed\u53e5\nstmt = cur.prepare(\"INSERT INTO info (code, topic, author) VALUES ($1, $2, $3)\")\n\n# \u6267\u884c\u8bed\u53e5\nstmt(\n    \"007\",\n    \"The Paron lal\",\n    \"pardon110|Candy\"\n)\n\n# \u63d0\u4ea4\u66f4\u6539\nconn.commit()\n\n# \u5173\u95ed\u8fde\u63a5\ncur.close()\nconn.close()<\/pre>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5982\u4f55\u5c06\u6570\u636e\u9ad8\u6548\u5bfc\u5165 PostgreSQL \u6570\u636e\u5e93\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\u5427\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5c06\u6570\u636e\u9ad8\u6548\u5bfc\u5165 Postgr&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-200683","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200683","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=200683"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200683\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}