The activerecord-copy gem adds a copy_from_client method to all your model classes, as shown in the snippet above (you’ll have to define the columns and their values as shown). foreach (filename, headers : true ) do |row | User. Task :single_record = > :environment do #This function loops over the content of the csv file and creates a new record for each of them. # lib/tasks/one_record_at_a_time.rake require 'csv' require "benchmark"ĭesc "imports data from csv to postgresql" Here’s a sample code to do this in Rails, assuming we have the source data in a CSV file: One easy way to load data to a Postgres database is to loop through the data and insert them one at a time. Other articles and resources you might like Speed comparison of different ways to load data into Postgres with Rails Using background jobs to load data to your Postgres databaseįinal Thoughts About Loading Large Data Sets into a PostgreSQL Database with Rails Cons of using PostgreSQL Copy with Activerecord-copyĤ.Pros of using PostgreSQL Copy with Activerecord-copy. Using PostgreSQL Copy with Activerecord-copy to load data to your Postgres database
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |