Search by

tpetry / laravel-postgresql-rls

tpetry

Row-level security for Laravel applications using PostgreSQL

Package info

github.com/tpetry/laravel-postgresql-rls

pkg:composer/tpetry/laravel-postgresql-rls

Statistics

Installs: 31

Dependents: 0

Suggesters: 0

Stars: 2

Open Issues: 0

1.0.0 2026-11-16 14:02 UTC

This package is auto-updated.

Last update: 2026-11-16 22:19:38 UTC


README

Laravel PostgreSQL Row-Level Security

Packagist PHP from Packagist Laravel versions GitHub Workflow Status (main) Total Downloads

Row-level security for Laravel applications using PostgreSQL

Installation

You can install the package via Composer:

composer require tpetry/laravel-postgresql-rls

Usage

Register an identity resolver, e.g. in the boot() method of your AppServiceProvider, which returns the claims for the RLS policies:

use Illuminate\Support\Facades\Auth;
use Tpetry\PostgresqlRls\Facades\RlsIdentityResolver;
use Tpetry\PostgresqlRls\RlsIdentity;

RlsIdentityResolver::register(fn (array $params, array $context) => new RlsIdentity(
    claims: ['tenant_id' => Auth::user()->tenant_id],
));

All queries within the authenticated() closure are executed within a transaction with those claims applied:

use Tpetry\PostgresqlRls\Facades\Rls;

$invoices = Rls::authenticated(function () {
    return Invoice::all();
});

The claims are available to your RLS policies as a JSON object in the rls.claims setting, e.g. for a policy created with the row-level security migrations:

use Tpetry\PostgresqlEnhanced\Schema\Blueprint;
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;

Schema::table('invoices', function (Blueprint $table) {
    $table->rowLevelSecurity();
    $table->createPolicy(
        name: 'tenant_isolation',
        user: 'app',
        condition: "tenant_id = (SELECT NULLIF(current_setting('rls.claims', true), '')::jsonb ->> 'tenant_id')::bigint",
    );
});

Changelog

Please see CHANGELOG for more information on what has changed recently.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

License

Laravel PostgreSQL Row-Level Security is open-sourced software licensed under the MIT license.