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 creating separate databases, try using different schemas.

    This means creating a different tenant for each customer within the same database. PostgreSQL natively supports the multitenancy concept, making it a good fit for this approach.

    If you plan to use Apartment Gem, that's an excellent choice for your requirements. It simplifies handling multi-tenant logic significantly.

    With this approach, you can keep each customer's data isolated from other customers. I have used this methodology, and it worked perfectly for me.

    Managing and scaling one database per customer can be very challenging.

    Hope this might helps you :)