I have a bunch of data in a CSV file that I want to host on a Heroku Postgres DB for my app to use as a reference. Its 4 columns and about 4,000 rows.
Whats the best way to get this data into my hosted DB?
Answers to your questions
I have a bunch of data in a CSV file that I want to host on a Heroku Postgres DB for my app to use as a reference. Its 4 columns and about 4,000 rows.
Whats the best way to get this data into my hosted DB?
I would recommend writing a simple Rake task to do this. Assuming the data from the CSV is intended for a single table, and it’s a list of user names and email addresses.
task :import_from_csv => :environment do
CSV.foreach(‘path/to/csv’) do |row|
User.create(name: row[0], email: row[1])
end
end
You can run this on a local copy of your Heroku PG database. If there is already data in it then you’ll want to pull down a copy before running the rake task.
heroku pg:pull HEROKU_DATABASE_NAME local_db
Then you could run this from the command line in your local db like so:
rake import_from_csv
And finally, push a copy of your local database into the Heroku one like this:
heroku pg:push local_db HEROKU_DATABASE_NAME