Search code examples
ruby-on-railsrubymulti-tenantsaasmulti-database

How do I switch from 1 database to 1 database per subdomain rails


I have a multi-tenancy app where everything is stored in 1 Postgres database. All models have a account_id, so the data a customer can see is based on his account_id. This works perfect, but due to security and privacy concerns, customers demand that their data is stored in a separate database. So what do I need to do to make this happen? When a custome logs in, then you connect to its own database? And what is the best way to migrate the current data to the several customer databases?

I try appartment gem


Solution

  • Instead of making seperate database try to do with different schema.

    It means create different tenant for each customer in db. In psql by default it supports the multitenancy concept.

    When you are planned to use apartment gem that was the best choice for requirement. Because it makes much easier when come to handling multi-tenant logics.

    With help of that you can separate each customer's data isolated from another customer. I have also used this methodology, it worked out for me very perfectly.

    One database per customer definitely maintaining and scaling is painful one.

    Hope this might helps you :)