Let’s say you are building a SAAS application system in which every user will use the database heavily, for example, a hospital management system. Should you have separate databases for each user? And how do you implement this in Django?
I generally advise against separate databases per user - it’s a lot to manage, and databases aren’t designed to be created in real-time. That said, there’s situations where it’s useful.
So consider:
-
If you have lots of small “organisations” and want quick onboarding, you should definitely just store everything in one big database and have an “organization id” column on all models you want to separate by organization (you may not want that on users, for example).
-
If you have few, big organisations, and you don’t mind it taking a couple of hours to add a new one, separate databases makes more sense. I would run these as totally separate Django installations, though - one install per organisation. You’ll have a lot more maintenance overhead (so many more places to apply migrations, do backups, etc.), which is why it only makes sense if you have a limited number.
There is a lot of great ressources out there on the subject, check this out : https://blog.usejournal.com/building-a-saas-application-with-django-web-framework-part-1-2-the-principe-2f0730a6693f
It show and explain how you can share data across multiples schemas of the same database.
A package like django-tenant from Tom Turner can help you archieve this.