{"id":2795,"date":"2018-06-27T16:09:35","date_gmt":"2018-06-27T08:09:35","guid":{"rendered":"http:\/\/blg.sofasay.com\/?p=2795"},"modified":"2018-06-28T11:52:08","modified_gmt":"2018-06-28T03:52:08","slug":"%e5%8a%a0%e8%bd%bdcsv%e5%88%b0postgresql","status":"publish","type":"post","link":"http:\/\/blog.sofasay.com\/?p=2795","title":{"rendered":"\u52a0\u8f7dcsv\u5230postgresql"},"content":{"rendered":"<p>\u6570\u636e\u8f6c\u79fb\u5e38\u7528csv\u5e73\u6587\u4ef6\uff0c\u4e00\u822c\u52a0\u8f7d\u8fdb\u6570\u636e\u5e93\u9700\u8981\u5148\u521b\u5efa\u8868\uff0c\u7136\u540e\u52a0\u8f7dcsv\u5230\u5bf9\u5e94\u7684\u8868\u91cc\u3002\u80fd\u4e0d\u80fd\u5728\u8868\u4e0d\u5b58\u5728\u7684\u60c5\u51b5\u4e0b\uff0c\u52a0\u8f7dcsv\u5230\u6570\u636e\u5e93\uff0c\u81ea\u52a8\u521b\u5efa\u8868\u5462\uff1f\u8fd9\u5bf9\u4e8e\u5feb\u901f\u5206\u6790\u5f88\u6709\u7528\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>We can use tools like <b>pgfutter<\/b> to import flat delimiter files into postgres. It will create the table automatically ( all column type is text) or append data to existing table.<\/p>\n<p>Refer to <a href=\"https:\/\/github.com\/lukasmartinelli\/pgfutter\/\">https:\/\/github.com\/lukasmartinelli\/pgfutter\/<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>\u52a0\u8f7dcsv\u5230postgrsql\u6570\u636e\u5e93\uff0c\u5982\u679c\u8868\u5b58\u5728\uff0c\u8ffd\u52a0\uff1b\u5982\u679c\u8868\u4e0d\u5b58\u5728\uff0c\u81ea\u52a8\u521b\u5efa\u4e0e\u6587\u4ef6\u540d\u540c\u540d\u7684\u8868\uff0c\u5b57\u6bb5\u9ed8\u8ba4\u90fd\u662ftext\u7c7b\u578b\u7684\u3002<\/p>\n<p><span style=\"font-family: Consolas;\">.\/pgfutter_linux_amd64 &#8211;host &#8220;<strong>hostname<\/strong>&#8221; &#8211;port &#8220;5432&#8221; &#8211;db &#8220;<strong>dbname<\/strong>&#8221; &#8211;schema &#8220;<strong>schemaNam<\/strong>&#8221;\u00a0 &#8211;user &#8220;<strong>username<\/strong>&#8221; &#8211;pw &#8220;<strong>password<\/strong>&#8221; csv All-Rewards.csv<\/span><\/p>\n<p>\u5728\u6570\u636e\u5e93\u4e0a\uff0c\u53ef\u4ee5\u505a\u6570\u636e\u7c7b\u578b\u7684\u8f6c\u6362\u3002\u53ef\u4ee5\u76f4\u63a5\u7f16\u5199\u67e5\u8be2sql\uff0c\u6216\u8005\u66f4\u6539\u5b57\u6bb5\u5c5e\u6027\u3002<\/p>\n<p><span style=\"font-family: Consolas;\">su &#8211; postgres <\/span><\/p>\n<p><span style=\"font-family: Consolas;\">psql -h hostname -d dbname -U username -c &#8220;alter table <b>schemaName.All_Rewards<\/b> alter column REWARD_CONCURRENCE type integer using (trim(REWARD_CONCURRENCE)::integer);&#8221;<\/span><\/p>\n<p><span style=\"font-family: Consolas;\">\\q<\/span><\/p>\n<p>\u6216\u8005\u5728SQL Client\u4e0a\u6539\uff1a<\/p>\n<p><span style=\"font-family: Consolas;\">alter table sao_paulo_20171011 alter column id type integer using (trim(id)::integer);<br \/>\nalter table sao_paulo_20171011 alter column price type integer using (trim(price)::numeric(10));<br \/>\nalter table sao_paulo_20171011 alter column update_time type timestamp\u00a0 using (trim(case when update_time =&#8221; then null else update_time end)::timestamp);<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>postgresql \u6570\u636e\u683c\u5f0f\u53c2\u8003\uff1a<\/p>\n<p>http:\/\/www.postgres.cn\/docs\/9.5\/datatype.html<br \/>\nhttps:\/\/www.postgresql.org\/docs\/9.5\/static\/datatype.html<\/p>\n<p>&nbsp;<\/p>\n<p>Linux\u7248\u672c\u4e0b\u8f7d\uff1a<\/p>\n<pre><a href=\"https:\/\/github.com\/lukasmartinelli\/pgfutter\/releases\/download\/v1.1\/pgfutter_linux_amd64\">https:\/\/github.com\/lukasmartinelli\/pgfutter\/releases\/download\/v1.1\/pgfutter_linux_amd64<\/a><\/pre>\n<pre><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6570\u636e\u8f6c\u79fb\u5e38\u7528csv\u5e73\u6587\u4ef6\uff0c\u4e00\u822c\u52a0\u8f7d\u8fdb\u6570\u636e\u5e93\u9700\u8981\u5148\u521b\u5efa\u8868\uff0c\u7136\u540e\u52a0\u8f7dcsv\u5230\u5bf9\u5e94\u7684\u8868\u91cc\u3002\u80fd\u4e0d\u80fd\u5728\u8868\u4e0d\u5b58\u5728\u7684\u60c5\u51b5\u4e0b\uff0c &hellip; <a href=\"http:\/\/blog.sofasay.com\/?p=2795\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u52a0\u8f7dcsv\u5230postgresql\u201d<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[13,464],"tags":[469],"views":845,"_links":{"self":[{"href":"http:\/\/blog.sofasay.com\/index.php?rest_route=\/wp\/v2\/posts\/2795"}],"collection":[{"href":"http:\/\/blog.sofasay.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.sofasay.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.sofasay.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.sofasay.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2795"}],"version-history":[{"count":0,"href":"http:\/\/blog.sofasay.com\/index.php?rest_route=\/wp\/v2\/posts\/2795\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.sofasay.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.sofasay.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2795"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.sofasay.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}