6

I recently deployed a Django web app on Elastic Beanstalk. I have configured it so that I can access the Django admin interface online and add content to the online site.

Now, the site is still under development - I will be chopping and changing and making tweaks, etc. Unfortunately, every time I deploy my app from the local version, the database (SQLite) is overwritten and any content I added on the online version is deleted.

Is there a way to 'pull' the database (and the database alone) from the online site? Alternatively could I tell the 'deploy' command to ignore the database?

Thanks folks.

Mark B
  • 183,023
  • 24
  • 297
  • 295
Jack Parkinson
  • 681
  • 11
  • 35
  • What database engine? MySQL, PostgreSQL? In any case you could just do something like mysqldump/restore like you would with any database running anywhere. – Mark B Apr 27 '16 at 17:39
  • Thanks for your comment, @Mark. It is just an SQLite one, and I am not sure how to access the online version of the database in Elastic Beanstalk. Is there a way to do that? – Jack Parkinson Apr 27 '16 at 17:59
  • If it's SQLite that's a totally different situation then (would have been good info to include in the original question). You would probably need to determine some way to deploy the data whenever you deploy the app. Are you limiting your Beanstalk application to a single EC2 instance? – Mark B Apr 27 '16 at 18:14
  • Sorry about that - have updated the question. I hadn't realised that it would be such a big issue - even if it takes a lot of effort, might it be better in the long run to move to MySQL or Postgres? And yes, my app is currently only on a single instance. I'm sorry, I'm very new to AWS and am not really sure of how it all fits together yet, so you might have to use small words. – Jack Parkinson Apr 27 '16 at 19:01
  • 1
    The main issue is that SQLite is an in-memory database that runs on the web server, which is quite a bit different from an RDS server that I thought you were talking about. And yes, in the long run it's probably best to switch to MySQL or PostgreSQL and use Amazon's RDS service. – Mark B Apr 27 '16 at 19:05
  • have you you added the sqlite db to your .gitignore file? – Patrick Tutu Sep 18 '17 at 16:05

1 Answers1

3

As it already been said in several comments, since (probably) your SQLite database is a file in your project directory it is being replaced/deleted each time you deploy your application, that is why you lose all data between deploys.

For production instances (and specially for these PaaS services) you should use an external database (PostgreSQL, MySQL,etc.).

Answering your question more directly, and assuming you want to leave your setup as it is (at least when you asked the question), I see 2 ways you could save your DB between deployments. One is accessing by ssh to the instance and fetch the database file, the other is exporting you data using the dumpdata command from django and then load it using loaddata.

dethos
  • 3,336
  • 1
  • 15
  • 15
  • I eventually moved my database to a MySQL server and pointed both production and development versions to it, based on this and advice in the comments. Thanks. – Jack Parkinson Sep 23 '17 at 19:18
  • Although it is a bit outdated https://realpython.com/deploying-a-django-app-and-postgresql-to-aws-elastic-beanstalk/#elastic-beanstalk-vs-ec2 is a nice tutorial to start using postgreSQL in elastic beanstalk – Tomás Gomez Pizarro Dec 19 '22 at 22:42