<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dogukan Kardas</title>
    <description>The latest articles on DEV Community by Dogukan Kardas (@dogukankardas).</description>
    <link>https://dev.to/dogukankardas</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4230802%2F4672b165-07f1-4501-9b6b-3c1e5d060e5e.png</url>
      <title>DEV Community: Dogukan Kardas</title>
      <link>https://dev.to/dogukankardas</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dogukankardas"/>
    <language>en</language>
    <item>
      <title>Designing multi-tenant SaaS on PostgreSQL</title>
      <dc:creator>Dogukan Kardas</dc:creator>
      <pubDate>Fri, 06 Nov 2026 16:25:01 +0000</pubDate>
      <link>https://dev.to/dogukankardas/designing-multi-tenant-saas-on-postgresql-1j0</link>
      <guid>https://dev.to/dogukankardas/designing-multi-tenant-saas-on-postgresql-1j0</guid>
      <description>&lt;p&gt;When I started Aurora Hub, the first irreversible decision was tenant isolation. Dealers, service teams, and admins all share one platform, but a dealer must never see another dealer’s customers. This article walks through the actual strategy running in production.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three isolation models, one winner
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Database-per-tenant: strongest isolation, but operationally heavy — migrations multiply by tenant count.&lt;/li&gt;
&lt;li&gt;Schema-per-tenant: middle ground, but connection pooling and cross-tenant reporting get awkward fast.&lt;/li&gt;
&lt;li&gt;Shared schema + tenant_id column with row-level security: one migration path, native Postgres enforcement.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I chose the shared schema with row-level security (RLS). The deciding factor was not performance — it was that RLS moves the isolation guarantee from application code into the database itself. A forgotten WHERE clause in a late-night query cannot leak data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Row-level security in practice
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Tenant policy on the customers table&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="n"&gt;ENABLE&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt; &lt;span class="k"&gt;LEVEL&lt;/span&gt; &lt;span class="k"&gt;SECURITY&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="n"&gt;POLICY&lt;/span&gt; &lt;span class="n"&gt;tenant_isolation&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;
  &lt;span class="k"&gt;USING&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tenant_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;current_setting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app.tenant_id'&lt;/span&gt;&lt;span class="p"&gt;)::&lt;/span&gt;&lt;span class="n"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;-- Every pooled connection sets the tenant before queries run&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tenant_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'9f3c2e1a-...'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The API layer sets app.tenant_id once per request from the verified JWT, inside a transaction. Every table with tenant data carries the same policy. Even raw ad-hoc queries through the same role are automatically scoped.&lt;/p&gt;

&lt;h2&gt;
  
  
  Index design for tenant-scoped queries
&lt;/h2&gt;

&lt;p&gt;With a shared schema, almost every query filters by tenant_id first. Composite indexes leading with tenant_id keep queries on a single tenant’s slice of the B-tree.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Composite indexes lead with tenant_id&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_customers_tenant_created&lt;/span&gt;
  &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tenant_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_tasks_tenant_status&lt;/span&gt;
  &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;tasks&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tenant_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s1"&gt;'archived'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What I would repeat and what I would change
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Repeat: RLS from day one. Retrofitting policies onto live tables is far riskier.&lt;/li&gt;
&lt;li&gt;Repeat: a tenant_id on every row, even for "obviously global" tables that later became tenant-specific.&lt;/li&gt;
&lt;li&gt;Change: I would add pgTAP tests for the policies earlier — I wrote them only after the first near-miss in staging.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://dogukankardas.com/writing/multi-tenant-saas-postgresql" rel="noopener noreferrer"&gt;dogukankardas.com&lt;/a&gt;. The full case study is here: &lt;a href="https://dogukankardas.com/projects/aurora-hub" rel="noopener noreferrer"&gt;Aurora Hub&lt;/a&gt;. I build software for small businesses and SaaS teams — see more at &lt;a href="https://dogukankardas.com" rel="noopener noreferrer"&gt;dogukankardas.com&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>saas</category>
      <category>architecture</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
