1

I'm new to Django but I'm deploying a Django-based website to Heroku, using Postgresql. The deployment was successful, and the website is online and has established connection with the database. However, none of the data from my local database has migrated to the heroku database, causing it to be a blank database. If I go into the admin section and manually input a datapoint, it appears on my site, so I know that database is correctly serving data. What is the proper way for migrating data from your local database to your online, heroku version of the database? I thought the following code would migrate the data:

heroku run python manage.py makemigrations
heroku run python manage.py migrate

But apparently I'm missing something.

  • Create the migration files locally and push to Heroku. It works, and it's better practice. BTW: this question has been asked [here](https://stackoverflow.com/q/43689548/2715819) and [here](https://stackoverflow.com/q/38330432/2715819). This comment applies to the edited content of the question, not the title or the original stated question. – RishiG Jan 04 '19 at 20:48
  • Thank you for pointing those out. Those posts didn't quite have a specific solution, so I asked again. I ended up using fixtures to prepopulate my database, per Robert's suggestion below. – somesurgeon Jan 06 '19 at 03:53
  • By the way, what did you mean by creating the migration files locally and pushing to Heroku? I tried "python manage.py migrate" after making migrations and did a "git push heroku master" but none of the data transferred to my database. I'm assuming by creating the migration files locally, you meant to use fixtures, and what I did aligns with your instructions. – somesurgeon Jan 06 '19 at 04:00
  • Sorry, I misread your question. Since you stated that you ran `makemigrations` on Heroku, I thought the problem was the automatically generated migration files themselves (which set up the structure of the database). For moving data around, the three options mentioned by Robert H are the best. – RishiG Jan 06 '19 at 14:50

1 Answers1

1

make migrations will create a migration that contains your schema, but no data. The migrate command applies the migration to the database.

In order to provide data to be sent over as part of the migrate command you need to either create a data migration or use a fixture.

Another option you have is to dump your local database and do an import into Heroku Postgres

All in all, it depends on how much local data you have that you want copied over. If its only a few rows, I would use either a data migration or a fixture, if its 100s or 1000s of rows an export/import of your dataset is your best bet.

Robert H
  • 11,520
  • 18
  • 68
  • 110
  • Thank you for the answer. I ended up using fixtures to prepopulate my database. So far, it seems to handle my number of rows without problem. Because the data is converted to JSON, and since JSON can be quite stable even with a lot of data, I'm hoping it won't be too much of an issue. And when it does, I'm hoping that I can then break the data into chunks, and load them up as separate fixtures. That's a lot of "I hope," so we will have to see! – somesurgeon Jan 06 '19 at 03:52
  • No problem, and glad to have helped - if you find this answer has helped you, or resolved your issue please use the up (or down arrows) to indicate helpfulness and the checkmark to accept the answer. Best of luck! – Robert H Jan 07 '19 at 14:54