Which database engine to choose for Django app?

Question:

I’m new to Django and have only been using sqlite3 as a database engine in Django. Now one of the applications I’m working on is getting pretty big, both in terms of models’ complexity and requests/second.

How do database engines supported by Django compare in terms of performance? Any pitfalls in using any of them? And the last but not least, how easy is it to switch to another engine once you’ve used one for a while?

Asked By: Ivan Vashchenko

||

Answers:

If you are going to use a relational database, the most popular in the Django community seems to be PostgreSQL. It’s my personal favorite. But, MongoDB seems to be getting pretty popular in the Python/Django community as well (I have never done a project with it, though). There are a lot of successful projects out there on MySQL as well. But, I personally prefer PostgreSQL 9.0 or 9.1. Hope this helps.

EDIT: I didn’t do that great of a job with this post. Just want to add a couple of more considerations.

For the vast majority of websites, either MySQL or PostgreSQL will work fine. Both have their strengths and weaknesses. I suggest you google “MySQL vs. PostgreSQL” There are a lot of hits for this search (at the time writing this, I get over 3,000,000). Here are a few tips in doing your evaluation.

  1. Give strong preference to more recent articles. Try to make sure you are comparing MySQL 5.5 to PostgreSQL 9.0 or 9.1.
  2. MySQL let’s you choose your storage engine. IMO, the closes Apple to Apples comparison is InnoDB to Postgres.
  3. Keep in mind that you may not need all of the features of InnoDB or Postgres. You should also look at some of the other Storage engines.

Also, if you plan on using any triggers in your system, there a couple of really nasty bugs with MySQL and InnoDB related to them and ACID compliance. Here’s the first one and here is another one. You may not need this functionality, just be aware of it.

One last thing that might make a difference to you is that with PostgreSQL you can write db functions with Python. Here is a link to the docs for this.

Answered By: David S

MySQL and PostgreSQL work best with Django. I would highly suggest that when you choose one that you change your development settings to use it while development (opposed to using sqlite3 in dev mode and a “real” database in prod) as there are subtle behavioral differences that can caused lots of headaches in the future.

Answered By: guidoism

MySQL and Postgres are the two most common DB backends used in the Django community and have comparable performance. I would agree that Postgres is more popular in the Django community though I don’t have any hard numbers to back that up. I certainly don’t mean to pick on MySQL but I would say there are some common pitfalls when using MySQL with Django (or MySQL in general):

  1. No transaction support with MyISAM (no longer the default in 5.5.5)
  2. No millisecond support for datetimes
  3. No timezone support for datetimes
  4. Unique character fields must be less than 255 characters
  5. Default collation is case-insensitive

There are some docs on the various features of Django which aren’t supported on various DB backends: https://docs.djangoproject.com/en/1.3/ref/databases/.

Answered By: Mark Lavin

I am not sure which DB to use either but if anyone is planning to use MongoDB then, be aware that it only works for py2 and not for py3.

Reference Links:

Project Setup with Django 1.10, mongodb and Python 3.4.3

Error while setting up MongoDB with django using django mongodb engine on windows

Setting up MongoDB + Django

Answered By: Algor Troy
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.