Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku app database resetting

Tags:

django

heroku

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?

like image 207
jazzabeanie Avatar asked Feb 17 '16 12:02

jazzabeanie


1 Answers

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

like image 51
ilse2005 Avatar answered Sep 28 '22 08:09

ilse2005