After running through Getting Started with Python on Heroku, I launched my first app. Everything seems to be working fine, but after a little while (a few hours maybe), the database resets. My hypothesis of the root cause is that my django app is using the default django database (SQLite I think) and Heroku supports postgres by default. I haven't tested this because it seems like quite a lot of work to change my app to postgres and it's something I'd rather not do now if I don't have to.
In summary, my question is, is my database not saving due to my app using SQLite? And if so, why does my app work at all? if not, where is the first place I should look to solve the issue?
As stated by @DanielRoseman you should not use SQLite on heroku, because SQLite runs in memory, and backs up its data store in files on disk. Let me quote from the heroku doku:
SQLite runs in memory, and backs up its data store in files on disk. While this strategy works well for development, Heroku’s Cedar stack has an ephemeral filesystem. You can write to it, and you can read from it, but the contents will be cleared periodically. If you were to use SQLite on Heroku, you would lose your entire database at least once every 24 hours.
Even if Heroku’s disks were persistent running SQLite would still not be a good fit. Since SQLite does not run as a service, each dyno would run a separate running copy. Each of these copies need their own disk backed store. This would mean that each dyno powering your app would have a different set of data since the disks are not synchronized.
Instead of using SQLite on Heroku you can configure your app to run on Postgres.
It is really easy to use postgres in django. You only have to change the database adapter in your settings file:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydb',
'USER': 'myuser',
'PASSWORD': 'password',
'HOST': 'localhost', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
There is also a tutorial on how to setup a django application on herku in their docu section:
https://devcenter.heroku.com/articles/django-app-configuration
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With