I succesfully deployed my first Django/Heroku app, and now I only need to transfer my database. It was previously on a MySql db on a Win7 PC. I looked around for ways to import csv into the Heroku db but didn't find anything. They suggest using a ruby gem to do it, or using taps and this command:
heroku db:push mysql://root:mypass@localhost/mydb.
My database is pretty small, only around 1000 columns and 2 tables, so it would be pretty simple to do it import the CSV files, but I cant find how to do it. Anyone knows?
3 Answers
Here's a few ideas that should get you going very quickly:
First, a quick and dirty approach:
- Install Postgres on your local machine.
- Import the CSV into a local Postgres database.
- Push that database to Heroku.
Alternatively, a slightly less quick and still a little dirty approach:
- Use the
CSV readerPython module. - Create a task in your Django app that loads the CSV, iterates over each CSV row, creates a new corresponding model, and saves it in the server's database if the model is valid.
- Run the task via the heroku CLI.
- Once you're finished and you've verified the server database, you can remove the task and CSV from the repo.
Otherwise, taps may suit you well too!
Comments
As per enter link description here, you can use the copy command to load a CSV into postgres from you local filesystem. You should be able to use this with your Heroku DB with something similar to:
PGPASSWORD=passwordhere psql -h hostname -U username dbname -c "\copy ..."
Comments
You can import a local csv file as a table in your heroku postgres by the below command
PGPASSWORD=<your password> psql -h <your heroku host> -U <heroku user> <heroku postgres database name> -c "\copy bank (ifsc, bank_id, branch, address, city, district, state, bank_name) FROM '<local file path location>' CSV HEADER DELIMITER E'\t';"
Please alter the DELIMITER value according to your needs. The 'E' before delimiter value is to denote that the command contains escape characters, otherwise it wil; throw exception