Heroku: Django database file lost on every dyno restart - django

I deployed a Django app on Heroku. I have some models and inserted some data to the database (SQLite database). But, when I tried to access the data after certain time, it showing an empty database. I found a problem similar to my issue here ->django heroku media files 404 error and I understood that, I should keep my Media files somewhwere else. Here my problem is with database and my question is, can I prevent my SQLite database from this data loss ?

There is nothing you can do about this, short of storing the database on some other service like Amazon S3. Heroku has ephemeral storage: https://devcenter.heroku.com/articles/sqlite3#disk-backed-storage
However, Heroku also comes with free persistent PostgreSQL. So I would advice you to use that instead.

Related

Does a Django site need a Postgres database when hosting on Heroku? Can I remove the Postgres add-on?

I have a website that I built using Django and deployed on Heroku. The tutorial I followed had me set up a Heroku Postgres database attached as an add-on. After Heroku's recent pricing changes the Postgres database has gone from free to $5 / month.
Maybe static isn't the correct word, but the site doesn't have user accounts, collect user information, or otherwise have any need for a database. I believe the only information that's stored in the database is login info for the Django admin site. I was able to export a .pgdmp file from Heroku of the data stored in the database but couldn't make heads or tails of the contents.
Here's the settings.py file code for the database section:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
My question is can I delete this add-on from the Heroku CLI or dashboard without breaking the site in production? Would I need to change anything in my settings.py file or elsewhere in my Django code first? I assume I would still be able to access the Django admin page on the development server, is that assumption correct?
Sorry for the possibly obvious question but I just don't want to break a site in production without doing some research first!
My question is can I delete this add-on from the Heroku CLI or dashboard without breaking the site in production?
We can't know for sure, but probably not.
You are using dj-database-url to set your default database. That likely means that your application is connecting to PostgreSQL via the DATABASE_URL environment variable.
SQLite does not work properly on Heroku due to its ephemeral filesystem. If you need a database, it needs to be a client-server database instead of a file-based database.
For anyone in my situation finding this in the future here's what I learned:
My initial hypothesis was correct, deleting the Postgres Heroku add-on left the site in production intact and working correctly. The only difference without the database is I can't access the Django admin page on the hosted site but I can still access this through the development server if needed.
Your site might be different than mine however, so your mileage may vary. Here's a couple things I did before making any changes to the site in production.
Check what tables/data you have in your database. Thanks to #Chris who answered in this thread and posted this link talking about the dataclips feature in Heroku. If you have tables that seem not to relate to the admin interface then you might be reliant on your database.
Consider spinning up another instance of your live site in production on Heroku. This way you can test and make changes without impacting users on the live site if something goes wrong.

Django transfer database to different Django project without messing up the logic

I built a Django project with a few models. Now I created a second server with the same setup. This one is meant to be the deployment server. Databases are separate from the dev-server.
However can you tell me if I can simply copy the databases from the dev server to the deploy or will the Django logic, since I also mean the user models and permissions etc.
The tables which I created myself are no problem to transfer to the new server. However I am wondering If Django gets confused when I also transfer something like the auth_user Model.
Should this work since I also just copied the backend logic as well?

Django Multiple Databases Set Up

I am trying to configure my django site to utilise two databases. My site is deployed via Heroku and I have a 'follower' database which is a read-only copy of my main database. As per my understanding from the Heroku docs, any changes to my main database are streamed live to the follower database.
So what I am trying to achieve is making all read operations go to the follower database and write operations to hit the main database.
Any help would be amazing.
Cheers,
Tom

Can django background tasks be deleted directly from the database?

We use django-background-tasks in our application and I have had no issue with deleting tasks from the admin page when running the app in my system. But when I tried to delete some tasks from the production version (hosted with gcp), I keep getting internal server (500) errors. What could be the reason for this?
Alternately, is it advisable/safe to directly delete the tasks from the background_task table in the database?
I deleted the tasks from the background_task table and it hasn't caused any issues.
The 500 could be due to some access issues (maybe some settings have to be configured to allow table modifications via admin page, over ssh?).

Django: Upload database associated file to remote server

Hello community of stack overflow.
Im having a big problem.
Well this is my scenery: I have two projects of Django, and the users model have a ImageField, When I create in both Porjects the users I want upload this field to another server, for example I have a Droplet in DigitalOcean just with apache, and I want upload all the media data in this server!
Thanks in advance!
I finally used the storage application, at the end, the system is uploading media files to AWS S3!