Courses/SQL/Multi-Tenant Databases

    Lesson 47 • Advanced

    Designing Multi-Tenant Databases

    Build SaaS databases that safely isolate tenant data using shared tables, RLS, or schema-per-tenant.

    ✅ What You'll Learn

    • • Shared tables with tenant_id column
    • • Row-Level Security for automatic isolation
    • • Schema-per-tenant architecture
    • • Choosing the right strategy for your SaaS

    🏢 Multi-Tenancy Explained

    🎯 Real-World Analogy: Think of an apartment building. Multi-tenancy is like multiple families living in the same building. Shared tables = everyone shares the lobby but has locked apartments (tenant_id). Schema-per-tenant = each family gets their own floor. Database-per-tenant = each family gets their own building.

    🔗 Shared Tables

    Cheapest. Easiest. Risk: data leaks if you forget tenant_id.

    📂 Schema-per-Tenant

    Better isolation. Moderate cost. Harder migrations.

    🏠 DB-per-Tenant

    Full isolation. Most expensive. Best for compliance.

    Try It: Shared Tables with tenant_id

    Design a multi-tenant schema using a tenant_id column on every table

    Try it Yourself »
    SQL
    -- Strategy 1: Shared Tables with tenant_id
    -- All tenants share the same tables, separated by a column
    -- Simplest to manage, most cost-effective
    
    CREATE TABLE tenants (
        tenant_id SERIAL PRIMARY KEY,
        name VARCHAR(100) NOT NULL,
        subdomain VARCHAR(50) UNIQUE NOT NULL,
        plan VARCHAR(20) DEFAULT 'free',
        created_at TIMESTAMPTZ DEFAULT NOW()
    );
    
    CREATE TABLE projects (
        project_id SERIAL PRIMARY KEY,
        tenant_id INT NOT NULL REFERENCES tenants(tenant_id),
        name VARCHAR(200)
    ...

    Try It: Row-Level Security Isolation

    Use RLS policies to automatically filter data by tenant

    Try it Yourself »
    SQL
    -- Row-Level Security for automatic tenant isolation
    -- The database itself prevents cross-tenant access!
    
    -- Enable RLS on tenant tables
    ALTER TABLE projects ENABLE ROW LEVEL SECURITY;
    ALTER TABLE tasks ENABLE ROW LEVEL SECURITY;
    
    -- Set tenant context (called at connection start)
    -- SET app.current_tenant = '42';
    
    -- Create RLS policies
    CREATE POLICY tenant_isolation_projects ON projects
        USING (tenant_id = current_setting('app.current_tenant')::INT);
    
    CREATE POLICY tenant_isolation_tasks O
    ...

    Try It: Schema-per-Tenant

    Give each tenant their own PostgreSQL schema with dynamic creation

    Try it Yourself »
    SQL
    -- Strategy 2: Schema-per-Tenant
    -- Each tenant gets their own PostgreSQL schema
    -- Better isolation, slightly more complex
    
    -- Create tenant schema
    CREATE SCHEMA tenant_acme;
    CREATE SCHEMA tenant_globex;
    
    -- Create tables in each schema
    CREATE TABLE tenant_acme.projects (
        project_id SERIAL PRIMARY KEY,
        name VARCHAR(200) NOT NULL,
        created_at TIMESTAMPTZ DEFAULT NOW()
    );
    
    CREATE TABLE tenant_acme.tasks (
        task_id SERIAL PRIMARY KEY,
        project_id INT REFERENCES tenant_acme.project
    ...

    📋 Quick Reference

    StrategyIsolationCostComplexity
    Shared + tenant_idLow💰Simple
    Shared + RLSMedium💰Moderate
    Schema-per-tenantHigh💰💰Moderate
    DB-per-tenantComplete💰💰💰Complex

    🎉 Lesson Complete!

    You can now design multi-tenant SaaS databases! Next, learn about data warehousing with star and snowflake schemas.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy PolicyTerms of Service