Database Schemas
SaaS Multi-Tenant PostgreSQL Schema with Row-Level Security and Tests
B2B SaaS Postgres schema: tenants, roles, billing, API keys, audit log, 32 RLS policies, 18 isolation tests, seed data and a generated ERD.
What you get: 1 file · 29.99 KB · ZIP
See purchase optionsProduct description
A ready-to-run PostgreSQL schema for a multi-tenant B2B SaaS where every customer organisation shares one database. Tenant isolation is enforced by PostgreSQL row-level security and composite foreign keys, so a missing WHERE clause in your code cannot leak another tenant's data. Plain SQL, no ORM lock-in.
What is inside
- 6 ordered SQL migrations: extensions and app role, tenancy (tenants, users, memberships with owner/admin/member/viewer roles, invitations), billing (plans, subscriptions, invoices), example domain (projects, tasks, comments, attachments), API keys and webhooks, audit log, and RLS
- 14 tables, 22 foreign keys, 32 row-level security policies
- Functions for the moments before a tenant is chosen: sign-up (create_tenant), accept_invitation by token, my_tenants for a tenant switcher, resolve_api_key (SHA-256 lookup, only hashes stored)
- seed/seed.sql: 2 tenants, 5 users (one in both tenants), plans, subscriptions, invoices and demo rows
- tests/rls_test.sql: 18 tests run as the real application role, each rolled back, with expected output
- docs/erd.svg and erd-dark.svg, generated from the live database catalog by the included tools/erd.py (Python 3.8+ and psql, no pip packages)
- migrate.sh to apply migrations, seed and tests in one command, plus a README with the context pattern, role matrix and ORM notes
What the tests prove
- A tenant sees, updates and deletes only its own rows; lookups of foreign ids return nothing
- Inserts with a foreign tenant_id and rows moved between tenants are rejected
- A child row cannot point at a parent of another tenant (composite FK)
- No tenant context means no rows: forgetting to set it fails closed
- Viewers cannot write, members cannot see billing or API keys, admins cannot create or remove owners
- The audit log is append-only; invitations grant exactly the invited role
Requirements and setup
PostgreSQL 14, 15, 16 or 17 (tested on 15.14), including managed services. Run DATABASE_URL=... ./migrate.sh --seed --test and expect 18 PASS lines. Your app sets app.tenant_id and app.user_id with set_config at the start of each transaction, which is safe behind pgbouncer in transaction mode.
Licence
Commercial licence: use it in unlimited own and client projects, modify it freely. Reselling or redistributing the schema itself as a template is not allowed.
What you get
1 file · 29.99 KB
- SaaS-Multi-Tenant-PostgreSQL-Schema.zipZIP · 29.99 KB
Database Schemas
SaaS Multi-Tenant PostgreSQL Schema with Row-Level Security and Tests
B2B SaaS Postgres schema: tenants, roles, billing, API keys, audit log, 32 RLS policies, 18 isolation tests, seed data and a generated ERD.
Getly opened the uploaded files and found nothing that would stop them opening. It is not a virus scan or a quality rating.
Tags
Frequently asked questions
Do I get access instantly?
Yes. This is a digital product - you can download it right after checkout, straight from your library.
Can I use it for commercial projects?
Yes. Commercial use is allowed: use it in your own business and in things you sell. You may not resell or redistribute the file itself.
What's your refund policy?
You can request a refund within 30 days. If something isn't right, request it from your order and the Getly team reviews it.
What file formats and sizes will I get?
You'll download 1 file - ZIP - 29.99 KB in total.
Do I get free updates?
Yes - when the seller ships a new version, you can re-download the latest files from your library at no extra cost. Check the changelog for recent updates.
Price
$19.00



