This is an example repository of achieving Multi-Tenancy in Golang using Row Level Security (RLS) in Postgres. Companion article: multi-tenancy-with-rls.
docker-initdb
contains all the necessary SQL commands required to initialize our containarized database.- For demonstration purposes,
tenantId
is dynamically set via query param and updated via request context. - While configuring the database connection, we are dynamically setting the
app.current_tenant
configuration paramter usingset_tenant
SQL function usingBeforeAcquire
hook from pgx library. - Similarily, we will reset the tenantId using
AfterRelease
hook.
- Run
docker compose up
- Wait few seconds for
apiserver
to be live as it waits until database is intialized. - Run
curl -X GET http://localhost:8848/products\?tenantId=<1|2>
to dynamically set the tenantId values.