https://github.com/cychiuae/casbin-pg-adapter
PostgreSQL adapter for Casbin
https://github.com/cychiuae/casbin-pg-adapter
casbin casbin-adapter go postgres-adapter
Last synced: 5 months ago
JSON representation
PostgreSQL adapter for Casbin
- Host: GitHub
- URL: https://github.com/cychiuae/casbin-pg-adapter
- Owner: cychiuae
- License: apache-2.0
- Created: 2019-12-09T02:51:17.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2021-01-28T06:52:10.000Z (over 5 years ago)
- Last Synced: 2025-08-14T20:44:20.012Z (10 months ago)
- Topics: casbin, casbin-adapter, go, postgres-adapter
- Language: Go
- Size: 2.99 MB
- Stars: 9
- Watchers: 2
- Forks: 6
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Casbin Postgres Adapter
Casbin Postgres Adapter is the postgres adapter for [Casbin](Casbin)
## Installation
```sh
$ go get github.com/cychiuae/casbin-pg-adapter
```
## Example
```go
package main
import (
"database/sql"
"os"
"github.com/casbin/casbin/v2"
"github.com/cychiuae/casbin-pg-adapter"
)
func main() {
connectionString := "postgresql://postgres:@localhost:5432/postgres?sslmode=disable"
db, err := sql.Open("postgres", os.Getenv("DATABASE_URL"))
if err != nil {
panic(err)
}
tableName := "casbin"
adapter, err := casbinpgadapter.NewAdapter(db, tableName)
// If you are using db schema
// myDBSchema := "mySchema"
// adapter, err := casbinpgadapter.NewAdapterWithDBSchema(db, myDBSchema, tableName)
if err != nil {
panic(err)
}
enforcer, err := casbin.NewEnforcer("./examples/model.conf", adapter)
if err != nil {
panic(err)
}
// Load stored policy from database
enforcer.LoadPolicy()
// Do permission checking
enforcer.Enforce("alice", "data1", "write")
// Do some mutations
enforcer.AddPolicy("alice", "data2", "write")
enforcer.RemovePolicy("alice", "data1", "write")
// Persist policy to database
enforcer.SavePolicy()
}
```