<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Petar Ćurković</title>
    <description>The latest articles on DEV Community by Petar Ćurković (@petarcurkovic).</description>
    <link>https://dev.to/petarcurkovic</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F32541%2Fbe98cb19-3ec5-4d3c-ae96-1e6f914e28d8.jpeg</url>
      <title>DEV Community: Petar Ćurković</title>
      <link>https://dev.to/petarcurkovic</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/petarcurkovic"/>
    <language>en</language>
    <item>
      <title>Superfast CSV imports using PostgreSQL's COPY command</title>
      <dc:creator>Petar Ćurković</dc:creator>
      <pubDate>Thu, 07 Sep 2017 07:49:27 +0000</pubDate>
      <link>https://dev.to/petarcurkovic/superfast-csv-imports-using-postgresqls-copy-command</link>
      <guid>https://dev.to/petarcurkovic/superfast-csv-imports-using-postgresqls-copy-command</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was originally published on &lt;a href="https://infinum.co/the-capsized-eight/superfast-csv-imports-using-postgresqls-copy" rel="noopener noreferrer"&gt;The Capsized Eight Blog&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Dealing with various sources of data in web applications requires us to create services that will extract information from CSV, Excel, and other file types. In that case, it's best to use some existing libraries, or if your backend is on Rails, use gems. There are many gems with very cool features like &lt;a href="https://github.com/pcreux/csv-importer" rel="noopener noreferrer"&gt;&lt;code&gt;CSVImporter&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://github.com/roo-rb/roo" rel="noopener noreferrer"&gt;&lt;code&gt;Roo&lt;/code&gt;&lt;/a&gt;. But you can also use plain &lt;a href="http://ruby-doc.org/stdlib-2.0.0/libdoc/csv/rdoc/CSV.html" rel="noopener noreferrer"&gt;&lt;code&gt;Ruby CSV&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Either way, if those are small CSV files, you will get your job done easily. But what if you need to import large CSV files (~100MB / ~1M rows)? &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fr5mjhydxlvwtb4j5x3i7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fr5mjhydxlvwtb4j5x3i7.png" alt="Postgres COPY"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I found that PostgreSQL has a really powerful yet very simple command called &lt;a href="https://www.postgresql.org/docs/9.6/static/sql-copy.html" rel="noopener noreferrer"&gt;&lt;code&gt;COPY&lt;/code&gt;&lt;/a&gt; which copies data between a file and a database table.&lt;br&gt;
It can be used in both ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;to import data from a CSV file to database&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;to export data from a database table to a CSV file&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example of usage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;COPY&lt;/span&gt; &lt;span class="n"&gt;forecasts&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="s1"&gt;'tmp/forecast.csv'&lt;/span&gt;
&lt;span class="n"&gt;CSV&lt;/span&gt; &lt;span class="n"&gt;HEADER&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This piece of SQL code will import the content from a CSV file to our &lt;code&gt;forecasts&lt;/code&gt; table. Note one thing: it's assumed that &lt;strong&gt;the number and order of columns in the table is the same as in the CSV file&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Results
&lt;/h3&gt;

&lt;p&gt;Importing a CSV file with ~1M rows now takes &lt;strong&gt;under 4 seconds&lt;/strong&gt; which is blazing fast when compared to previous solutions! &lt;/p&gt;

&lt;p&gt;For more details, read &lt;a href="https://infinum.co/the-capsized-eight/superfast-csv-imports-using-postgresqls-copy" rel="noopener noreferrer"&gt;the original blogpost&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>database</category>
      <category>backend</category>
      <category>rails</category>
    </item>
  </channel>
</rss>
