(Note · · 4 min read)
Multi-tenant SaaS on one Postgres database: keeping each company's data apart
Paylio, the HR and payroll SaaS I build, runs many companies on one PostgreSQL database. Their salaries sit in the same tables, so a single missed filter would show one company's payroll to another. These are the rules that stop that, in production since September 2026.
By Md. Habibur Rahman Shohel, full-stack & AI product engineer in Dhaka
One database, a company on every row
Every company's data lives in the same tables, and every row carries the company it belongs to. One set of tables means one schema to migrate and one database to load-test. The cost is that separation becomes the code's job: a query that forgets the company filter returns everyone's rows.
The rule lives in one layer
The FastAPI backend is layered: router, service, repository. Every service method that touches company data filters by the caller's organisation, so the rule sits in one place a reviewer can read instead of being repeated in every handler. At Paylio's size, about 1,200 API routes over about 200 tables, that is the difference between checking one layer and checking everything.
Check on the server, every request
Each request is checked for permission on the server, then limited to the caller's organisation. A hidden button is never the guard: the action is checked again when the request arrives. Permissions are fine-grained, with 139 of them, 7 built-in roles and custom roles each company defines for itself.
Encrypt what would hurt most if it leaked
Separation protects one company from another. Encryption protects employees if the data itself gets out. National ID, bank account, tax ID (TIN) and salary details are encrypted at rest. Searching them uses blind indexes: a keyed hash stored beside the encrypted value, so an exact match can be found without decrypting the rows.
Logs are structured JSON, and that personal data is kept out of them, out of traces and out of commits.
Prove it with tests and load
Every fix starts with a failing test, and about 4,900 backend tests run before anything reaches staging. Every change goes through a staging copy first.
One shared database is also one database to outgrow, so the limit is measured before a large company signs up, not after. A load-test harness seeds 10,000 employees and 420,000 attendance records. Profiling the slow paths cut CPU per request by 36%, and at 50 concurrent users the p95 response fell from 1.9 s to 207 ms on one API core (a laptop benchmark, 16 November 2026).
Common questions
How does a multi-tenant SaaS keep companies' data apart in one Postgres database?
By tagging every row with the company it belongs to and filtering every query by the caller's organisation. In Paylio that filter lives in one service layer, and every request is permission-checked on the server before it is limited to the caller's organisation.
Is hiding a button in the interface enough to protect data?
No. In Paylio every action is checked for permission again on the server when the request arrives; a hidden button is never the guard.
How do you search encrypted personal data?
With a blind index: a keyed hash stored beside the encrypted value, so an exact match can be found without decrypting the rows. Paylio encrypts national ID, bank account, TIN and salary details at rest and searches them with blind indexes.