Simon Rentzke

Dump csv reports using Ruby on Rails and Postgres

August 31, 2018

This method is the most efficient, super simple way to generate CSV files with Rails. It uses an active record connection and the Postgres COPY command writes each csv generated line from Postgres to a file., 'w') do |f|
        ActiveRecord::Base.connection.raw_connection.copy_data "COPY (SELECT * FROM users) TO STDOUT WITH (FORMAT CSV, HEADER TRUE, FORCE_QUOTE *);" do
          while line = connection.raw_connection.get_copy_data do
            f.write line.force_encoding('UTF-8')

Leveraging Postgres to do this, results in tens of millions of rows being able to be exported in under 5 mins.