https://github.com/jcasbin/jcasbin-postgres-watcher
PostgreSQL adapter for Casbin
https://github.com/jcasbin/jcasbin-postgres-watcher
adapter auth authorization authz casbin database java jcasbin middleware pg postgres postgresql storage-driver
Last synced: 12 months ago
JSON representation
PostgreSQL adapter for Casbin
- Host: GitHub
- URL: https://github.com/jcasbin/jcasbin-postgres-watcher
- Owner: jcasbin
- License: apache-2.0
- Created: 2025-06-20T14:08:41.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2025-06-25T01:36:41.000Z (12 months ago)
- Last Synced: 2025-06-25T02:36:24.220Z (12 months ago)
- Topics: adapter, auth, authorization, authz, casbin, database, java, jcasbin, middleware, pg, postgres, postgresql, storage-driver
- Language: Java
- Homepage: https://github.com/casbin/jcasbin
- Size: 15.6 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jcasbin-postgres-watcher
[](https://github.com/jcasbin/jcasbin-postgres-watcher/actions/workflows/ci.yml)

[](https://javadoc.io/doc/org.casbin/jcasbin-postgres-watcher)
[](https://mvnrepository.com/artifact/org.casbin/jcasbin-postgres-watcher/latest)
[](https://github.com/jcasbin/jcasbin-postgres-watcher/releases/latest)
[](https://discord.gg/S5UjpzGZjN)
jCasbin PostgreSQL Watcher is a [PostgreSQL](https://www.postgresql.org/) watcher for [jCasbin](https://github.com/casbin/jcasbin).
## Installation
**For Maven**
```
org.casbin
jcasbin-postgres-watcher
1.0.0
```
## Simple Example
if you have two casbin instances A and B
**A:** **Producer**
```java
// Initialize PostgreSQL Watcher
String channel = "casbin_channel";
JCasbinPostgresWatcher watcher = new JCasbinPostgresWatcher(
"jdbc:postgresql://localhost:5432/your_db",
"postgres",
"your_password",
channel
);
// Support for advanced configuration with WatcherConfig
// WatcherConfig config = new WatcherConfig();
// config.setChannel(channel);
// config.setVerbose(true);
// config.setLocalId("instance-1");
// JCasbinPostgresWatcher watcher = new JCasbinPostgresWatcher(url, user, password, config);
Enforcer enforcer = new SyncedEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv");
enforcer.setWatcher(watcher);
// The following code is not necessary and generally does not need to be written unless you understand what you want to do
/*
Runnable updateCallback = () -> {
// Custom behavior
};
watcher.setUpdateCallback(updateCallback);
*/
// Modify policy, it will notify B
enforcer.addPolicy(...);
// Using WatcherEx specific methods for fine-grained policy updates
// Add a policy
enforcer.addPolicy(...);
watcher.updateForAddPolicy(...);
```
**B:** **Consumer**
````Java
// Initialize PostgreSQL Watcher with same channel
String channel = "casbin_channel";
JCasbinPostgresWatcher watcher = new JCasbinPostgresWatcher(
"jdbc:postgresql://localhost:5432/your_db",
"postgres",
"your_password",
channel
);
Enforcer enforcer = new SyncedEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv");
enforcer.setWatcher(watcher);
// B set watcher and subscribe to the same channel, then it will receive the notification of A, and then call LoadPolicy to reload policy
````
## Getting Help
- [jCasbin](https://github.com/casbin/jCasbin)
- [pgjdbc](https://github.com/pgjdbc/pgjdbc)
## License
This project is under Apache 2.0 License. See the [LICENSE](https://github.com/jcasbin/redis-watcher/blob/master/LICENSE) file for the full license text.