A PostgreSQL adapter for Casbin using the pgx driver.
Casbin is a powerful and efficient open-source access control library that supports various access control models (ACL, RBAC, ABAC, etc.). This adapter enables Casbin to use PostgreSQL as its policy storage backend via the pgx driver, providing:
- Standard Casbin adapter interface
- Context-aware operations for request-scoped transactions
- Batch operations for efficient bulk policy updates
- Filtered adapter support for policy filtering
- Updatable adapter support for policy modifications
go get github.com/noho-digital/casbin-pgx-adapterpackage main
import (
"log"
"github.com/casbin/casbin/v3"
pgxadapter "github.com/noho-digital/casbin-pgx-adapter"
)
func main() {
// Database connection string
connStr := "postgres://postgres:postgres@localhost:5432/casbin?sslmode=disable"
// Create the adapter with optional configuration
adapter, err := pgxadapter.NewAdapter(connStr,
pgxadapter.WithTableName("my_casbin_rules"), // Optional: custom table name
pgxadapter.WithDatabaseName("my_casbin_db"), // Optional: custom database name
)
if err != nil {
log.Fatal("Failed to create adapter:", err)
}
// Create Casbin enforcer with model file and adapter
enforcer, err := casbin.NewEnforcer("path/to/model.conf", adapter)
if err != nil {
log.Fatal("Failed to create enforcer:", err)
}
// Add some policies
enforcer.AddPolicy("alice", "data1", "read")
enforcer.AddPolicy("bob", "data2", "write")
enforcer.AddGroupingPolicy("alice", "admin")
// Save policies to database
if err := enforcer.SavePolicy(); err != nil {
log.Fatal("Failed to save policy:", err)
}
// Check permissions
if allowed, _ := enforcer.Enforce("alice", "data1", "read"); allowed {
log.Println("Alice can read data1")
}
if allowed, _ := enforcer.Enforce("bob", "data1", "read"); !allowed {
log.Println("Bob cannot read data1")
}
}This adapter implements the following Casbin adapter interfaces:
Adapter- Standard Casbin adapter interfaceContextAdapter- Context-aware operationsBatchAdapter- Batch add/remove operationsFilteredAdapter- Policy filtering supportUpdatableAdapter- Policy update operations
This project includes a Docker Compose setup for running tests against a PostgreSQL database.
# Start the test database
make test-db-up
# Run tests
make test
# Stop test database when done
make test-db-down
# Set environment variable
export TEST_DATABASE_URL="postgres://postgres:postgres@localhost:5433/casbin_test?sslmode=disable"
# Run tests
go test -v ./...- Host: localhost:5433
- Database: casbin_test
- Username: postgres
- Password: postgres
- Connection String:
postgres://postgres:postgres@localhost:5433/casbin_test?sslmode=disable
- Go 1.24.4 or later
- Docker and Docker Compose (for development/testing)
- PostgreSQL database (for production)
- Casbin v3