こんにちは、サーバーサイドエンジニアーのユキチです。 SaaSの開発を行う上では複数の企業様のデータを扱うことになります。 そういったマルチテナントのデータ設計を行う上で、弊社ではPostgreSQLのRow Level Securityという機能を使って実装しました。 今回はPostgreSQLのRow Level Security(以下RLS)という機能を使ってマルチテナントのデータ操作を安全に扱う方法を紹介していきたいと思います。 マルチテナントデータベース設計 主に3パターンの設計手法が考えられます。 Multi Tenants in 1 Database:複数企業のデータを一つのデータベースで管理 1 Tenant in 1 Database:テナントごとにデータベースを分けて管理。物理的なインスタンスは同じ。 1 Tenant in 1 Instance:テナントごとにデータベ