{"id":19298243,"url":"https://github.com/casbin/casbin-pg-adapter","last_synced_at":"2025-08-03T19:32:47.210Z","repository":{"id":43406926,"uuid":"230539547","full_name":"casbin/casbin-pg-adapter","owner":"casbin","description":"A go-pg adapter for casbin","archived":false,"fork":false,"pushed_at":"2024-01-26T16:26:53.000Z","size":76,"stargazers_count":39,"open_issues_count":0,"forks_count":28,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-10-29T22:37:43.522Z","etag":null,"topics":["access-control","adapter","casbin","go","go-pg","golang","pg"],"latest_commit_sha":null,"homepage":"https://github.com/casbin/casbin","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/casbin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null},"funding":{"github":"casbin"}},"created_at":"2019-12-28T00:58:04.000Z","updated_at":"2024-10-27T07:08:55.000Z","dependencies_parsed_at":"2022-07-08T21:47:44.262Z","dependency_job_id":"295ce09c-e522-4d4b-8bd3-c64286b6d7d0","html_url":"https://github.com/casbin/casbin-pg-adapter","commit_stats":{"total_commits":44,"total_committers":20,"mean_commits":2.2,"dds":0.7954545454545454,"last_synced_commit":"1dea9515c42aa46b87c8ea21f5026baf0e318284"},"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin%2Fcasbin-pg-adapter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin%2Fcasbin-pg-adapter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin%2Fcasbin-pg-adapter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin%2Fcasbin-pg-adapter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/casbin","download_url":"https://codeload.github.com/casbin/casbin-pg-adapter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223175696,"owners_count":17100480,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["access-control","adapter","casbin","go","go-pg","golang","pg"],"created_at":"2024-11-09T23:07:22.740Z","updated_at":"2024-11-09T23:07:25.363Z","avatar_url":"https://github.com/casbin.png","language":"Go","funding_links":["https://github.com/sponsors/casbin"],"categories":[],"sub_categories":[],"readme":"# Go-pg Adapter\n\n[![Go](https://github.com/casbin/casbin-pg-adapter/actions/workflows/ci.yml/badge.svg)](https://github.com/casbin/casbin-pg-adapter/actions/workflows/ci.yml)\n[![Coverage Status](https://coveralls.io/repos/github/casbin/casbin-pg-adapter/badge.svg?branch=master)](https://coveralls.io/github/casbin/casbin-pg-adapter?branch=master)\n\nGo-pg Adapter is the [Go-pg](https://github.com/go-pg/pg) adapter for [Casbin](https://github.com/casbin/casbin). With this library, Casbin can load policy from PostgreSQL or save policy to it.\n\n## Installation\n\n    go get github.com/casbin/casbin-pg-adapter\n\n## Simple Postgres Example\n\n```go\npackage main\n\nimport (\n\tpgadapter \"github.com/casbin/casbin-pg-adapter\"\n\t\"github.com/casbin/casbin/v2\"\n)\n\nfunc main() {\n\t// Initialize a Go-pg adapter and use it in a Casbin enforcer:\n\t// The adapter will use the Postgres database named \"casbin\".\n\t// If it doesn't exist, the adapter will create it automatically.\n\ta, _ := pgadapter.NewAdapter(\"postgresql://username:password@postgres:5432/database?sslmode=disable\") // Your driver and data source.\n\t// Alternatively, you can construct an adapter instance with *pg.Options:\n\t// a, _ := pgadapter.NewAdapter(\u0026pg.Options{\n\t//     Database: \"...\",\n\t//     User: \"...\",\n\t//     Password: \"...\",\n\t// })\n\n\t// The adapter will use the table named \"casbin_rule\".\n\t// If it doesn't exist, the adapter will create it automatically.\n\n\t// Or you can use an existing DB by adding a second string parameter with your database name to the NewAdapter(), like this:\n\t// a, _ := pgadapter.NewAdapter(\"postgresql://username:password@postgres:5432/database?sslmode=disable\", \"your_database_name\") \n\t\n\te := casbin.NewEnforcer(\"examples/rbac_model.conf\", a)\n\n\t// Load the policy from DB.\n\te.LoadPolicy()\n\n\t// Check the permission.\n\te.Enforce(\"alice\", \"data1\", \"read\")\n\n\t// Modify the policy.\n\t// e.AddPolicy(...)\n\t// e.RemovePolicy(...)\n\n\t// Save the policy back to DB.\n\te.SavePolicy()\n}\n```\n\n## Support for FilteredAdapter interface\n\nYou can [load a subset of policies](https://casbin.org/docs/policy-subset-loading) with this adapter:\n\n```go\npackage main\n\nimport (\n\t\"github.com/casbin/casbin/v2\"\n\tpgadapter \"github.com/casbin/casbin-pg-adapter\"\n)\n\nfunc main() {\n\ta, _ := pgadapter.NewAdapter(\"postgresql://username:password@postgres:5432/database?sslmode=disable\")\n\te, _ := casbin.NewEnforcer(\"examples/rbac_model.conf\", a)\n\n\te.LoadFilteredPolicy(\u0026pgadapter.Filter{\n\t\tP: []string{\"\", \"data1\"},\n\t\tG: []string{\"alice\"},\n\t})\n\t...\n}\n```\n\n## Custom DB Connection\n\nYou can provide a custom table or database name with `pgadapter.NewAdapterByDB`\n\n```go\npackage main\n\nimport (\n\t\"github.com/casbin/casbin/v2\"\n\tpgadapter \"github.com/casbin/casbin-pg-adapter\"\n\t\"github.com/go-pg/pg/v9\"\n)\n\nfunc main() {\n\topts, _ := pg.ParseURL(\"postgresql://pguser:pgpassword@localhost:5432/pgdb?sslmode=disable\")\n\n\tdb := pg.Connect(opts)\n\tdefer db.Close()\n\n\ta, _ := pgadapter.NewAdapterByDB(db, pgadapter.WithTableName(\"custom_table\"))\n\te, _ := casbin.NewEnforcer(\"examples/rbac_model.conf\", a)\n    ...\n}\n```\n\n## Run all tests\n\n    docker-compose run --rm go\n\n## Debug tests\n\n    docker-compose run --rm go dlv test github.com/casbin/casbin-pg-adapter\n\n## Getting Help\n\n-   [Casbin](https://github.com/casbin/casbin)\n\n## License\n\nThis project is under Apache 2.0 License. See the [LICENSE](LICENSE) file for the full license text.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcasbin%2Fcasbin-pg-adapter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcasbin%2Fcasbin-pg-adapter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcasbin%2Fcasbin-pg-adapter/lists"}