Giter Site home page Giter Site logo

postgres-rls-example's Introduction

Postgres Row Level Security example

When developing a multi-tenant app, an important decision is how the data owned by each tenant will be isolated from each other. There are many approaches for this, like:

  • Allocating a dedicated database for every client
  • Using a single database but separated schemas for each client
  • Using a single database, single schema, and adding a tenant_id column in every table.

The last option cost less and it's easier to maintain than the others, but imposes some security challenges, like ensuring that one tenant can't access data from another tenant.

A good way to make this checking less error-prone, e.g., forgetting a where tenant_id = ? clause, is by centralizing the isolation policies at the database level, removing the responsibility from the developers. One way to achieve this on Postgres is to use a feature called Row Level Security, which allows to making restrictions on which rows are returned when selecting or affected when inserting, updating, and deleting.

This application is an example of how RLS can be used in an express app, using a middleware for getting the correct connection for each tenant.

Installation

git clone https://github.com/franzon/postgres-rls-example.git
cd postgres-rls-example
yarn

Usage

  1. Run a Postgres database (Docker can be used).
  2. Update credentials on knexfile.js
  3. Run migrations
npx knex:migrate latest
  1. Run seeds
npx knex:seed run
  1. Create an another Postgres and give permissions on database. (RLS will not work if using the same user used on migrations). Make sure to update knexfile.js.

  2. Run application

yarn dev
  1. Access this URL on browser

http://localhost:3000/users?tenant_id=tenant2

After acessing the URL, only the data related to tenant2 will be returned.

Contributing

Pull requests for code or README improvements are welcome.

License

MIT

postgres-rls-example's People

Contributors

dependabot[bot] avatar franzon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.