Simon Rentzke

Blue Blistering Barnacles

Dump csv reports using Ruby on Rails and Postgres

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.

      File.open(file_path, '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')
          end
        end
      end

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

Back to Blog