pg_casbin
pg_casbin · Casbin authorization models compiled into PostgreSQL queries
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 102473 | pg_casbin | pg_casbin | 0.5.0 | SEC | Apache-2.0 | Rust |
PostgreSQL · 17, 18
Usage
Sources:
- README.md
- Cargo.toml
- pg_casbin.control
- src/lib.rs
- src/sql/bootstrap.sql
- src/sql/finalize.sql
- docs/usage.md
- docs/logical_replication.md
- docs/migrate_0_3_to_0_4.md
- LICENSE
pg_casbin provides Casbin-compatible authorization inside PostgreSQL 17 and 18. The reviewed source revision declares version 0.5.0; no published GitHub release was found. It compiles supported model matchers into SQL over policy and role tables and can combine authorization checks with a query’s other filters.
Enablement and a basic model
Preload the library at session startup, preserving any existing entries. Session preload requires new connections; shared preload is an alternative that requires a server restart. Installing the extension requires a superuser. Its fixed schema is pgcasbin.
Requests are text arrays in model field order. Use the check in a WHERE or JOIN condition; it does not automatically protect every table or authenticate the supplied subject. Literal model and context arguments permit planner inlining. Unsupported model syntax raises an error: general ABAC property access, dynamic evaluation, custom functions and conditional role links are not supported.
Privileges and rule maintenance
The cluster-wide roles pgcasbin_reader, pgcasbin_writer and pgcasbin_admin respectively read rule and derived tables, also modify rules, and also manage models. The latter roles inherit the earlier roles. Everyone can call pgcasbin.enforce; checks read policies as the extension owner. Row-level policies on rule tables restrict writes, not the rules visible to checks.
Rule tables accept ordinary INSERT, UPDATE, DELETE, COPY and TRUNCATE. Statement triggers maintain derived role closures in the same transaction. Do not edit derived tables directly. Bulk statements avoid per-statement maintenance overhead; analyze rule tables after a large load. Changes emit notifications on casbin_policy.
pgcasbin.model_cache_size defaults to 64. pgcasbin.candidate_limit defaults to 1000 and zero disables candidate enumeration. pgcasbin.cache_ivm_plans defaults to off; enabling it caches maintenance plans within a session. Check the installed reference through pgcasbin.guide() and pgcasbin.guides().
Restore, upgrades and replication
After restoring a dump, run pgcasbin.refresh_model for each model to rebuild views, suggested indexes, role closures and extension dependencies. Rules are preserved. Moving from 0.3 to 0.4 replaces adapter write functions with ordinary rule-table SQL and requires reviewing grants and field names before upgrading.
Logical subscribers require the same extension version and preload setting. Replicate models before their rule tables, refresh subscriptions when new models add tables, and write only on the primary. Until every model table finishes synchronization, subscriber decisions may differ. Dropping the extension leaves the shared cluster roles behind.