Skip to main content
Ungated Database Architecture Tool

Free PostgreSQL Schema & ERD Generator

Generate production-grade relational database schemas with strict Row-Level Security (RLS) policies, foreign key cascades, and performant indexes. Engineered for multi-tenant SaaS, FinTech, and healthcare apps.

Multi-Tenant B2B SaaS Database Topology

Tenant isolation with organizations, memberships, role-based access, and Stripe subscriptions.

tenantsuserstenant_membershipssubscriptionsaudit_logsprojects
schema-saas.sql (PostgreSQL 15+)
UTF-8 · Ready to Execute
-- Multi-Tenant B2B SaaS Schema with PostgreSQL Row-Level Security (RLS)
-- Generated by AI Dev Planner (aidevplanner.com/tools/postgresql-erd-generator)

CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

-- 1. Tenants (Organizations)
CREATE TABLE tenants (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    slug VARCHAR(64) UNIQUE NOT NULL,
    name VARCHAR(255) NOT NULL,
    plan VARCHAR(32) NOT NULL DEFAULT 'starter',
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- 2. Users (Authenticated Identities)
CREATE TABLE users (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    email VARCHAR(255) UNIQUE NOT NULL,
    full_name VARCHAR(255),
    avatar_url TEXT,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- 3. Tenant Memberships (Roles & Access)
CREATE TABLE tenant_memberships (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    tenant_id UUID NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
    user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    role VARCHAR(32) NOT NULL DEFAULT 'member' CHECK (role IN ('owner', 'admin', 'member', 'billing')),
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    UNIQUE (tenant_id, user_id)
);

-- 4. Subscriptions & Billing
CREATE TABLE subscriptions (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    tenant_id UUID NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
    stripe_customer_id VARCHAR(255) UNIQUE,
    stripe_subscription_id VARCHAR(255) UNIQUE,
    status VARCHAR(32) NOT NULL DEFAULT 'trialing',
    current_period_end TIMESTAMPTZ,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- 5. Tenant Projects (Scoped Workspace Artifacts)
CREATE TABLE projects (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    tenant_id UUID NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
    name VARCHAR(255) NOT NULL,
    description TEXT,
    created_by UUID REFERENCES users(id) ON DELETE SET NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- Row-Level Security (RLS) Policies
ALTER TABLE projects ENABLE ROW LEVEL SECURITY;

CREATE POLICY tenant_isolation_policy ON projects
    FOR ALL
    USING (
        tenant_id IN (
            SELECT tenant_id FROM tenant_memberships
            WHERE user_id = auth.uid()
        )
    );

-- Fast Indexing
CREATE INDEX idx_memberships_user ON tenant_memberships(user_id);
CREATE INDEX idx_projects_tenant ON projects(tenant_id);
Production Engineering Handover

Need This Architecture Built and Deployed?

Our senior engineering team scopes, builds, and launches production SaaS and web platforms in fixed 2-week sprints with 100% full GitHub IP ownership.

Related: SaaS development services · MVP development services · published pricing