Startup-time validation in action
When PostgreSQL RLS configuration is missing or incorrect, the application fails at startup instead of failing later at runtime.
This prevents silent security assumptions and production-time surprises.
Example: RLS policy validation failure
Below is an example of what happens when a required RLS policy is missing or misconfigured:
At startup, the library validates:
- Whether the table exists
- Whether RLS is enabled on the table
- Whether the required policy exists
- Whether required session variables are configured
If any of these checks fail, the application does not start and provides a clear failure message.
Before we dive in, let’s be clear
This library does not manage database DDL.
- It does not create tables, policies, or RLS rules
- It does not require superuser or elevated database roles
- It only validates that the database security setup matches what the application expects
RLS policies and table configuration should be managed using standard database migration tools such as Flyway or Liquibase.
This is the recommended and preferred approach.
How startup validation works
RLS expectations are defined using annotations in JPA entities and configuration.
At application startup:
- The configuration is read
- The database state is inspected
- Validation errors are reported immediately
- If everything is valid, the application starts successfully
This ensures RLS issues are caught early, not under production load.
Binding the RLS session context
Session variables required by RLS policies are bound per transaction.
There are two supported ways to bind the session context:
1️⃣ Annotation-based binding (recommended)
Bind session variables directly on @Transactional method parameters.
2️⃣ Programmatic binding
Bind session variables manually using a provided context API.
Both approaches use transaction-scoped set_config(..., true), ensuring variables are automatically cleared after commit or rollback.
Getting started
The project is available on Maven Central:
io.github.aayushghimirey:jpa-postgres-rls:2.0.1
🔗 Maven Central
https://mvnrepository.com/artifact/io.github.aayushghimirey/jpa-postgres-rls/2.0.1
🔗 GitHub repository
https://github.com/aayushghimirey/jpa-postgres-rls



Top comments (0)