Handling large seed files in Rails with Postgres

Handling seed files in Rails is easy, but things can get slow when the files are too big.

I have a huge database that contains millions of city names, and it’s a pain in the ass to import it - it takes an average of 2 minutes to import the SQL file directly to Postgres. But Postgres is a cool RDBMS and it has nice functions such as COPY that can import data from files (such as CSV files) very fast - it only takes 20 seconds to import the same database in my rig.

Using the COPY function in Rails is pretty straightforward - you just need to set up your CSV seed file, write a custom SQL statement in your seed.rb file and voilà!

# Imports cities from CSV file
sql = "COPY core_locations (city) FROM '#{Rails.root}/db/seed/core_locations.csv'"
ActiveRecord::Base.connection.execute sql

I used to have Disqus enabled on my website, but I have disabled it because of privacy concerns.

If you feel like commenting or asking something, you can still contact me via other means.