{"id":22959846,"url":"https://github.com/pycasbin/postgresql-watcher","last_synced_at":"2025-08-13T05:32:41.347Z","repository":{"id":40265031,"uuid":"323272189","full_name":"pycasbin/postgresql-watcher","owner":"pycasbin","description":"PostgreSQL Watcher for PyCasbin","archived":false,"fork":false,"pushed_at":"2024-07-16T15:47:08.000Z","size":46,"stargazers_count":2,"open_issues_count":0,"forks_count":8,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-09-18T02:22:45.769Z","etag":null,"topics":["casbin","postgres","postgresql","pycasbin","watcher"],"latest_commit_sha":null,"homepage":"https://github.com/casbin/pycasbin","language":"Python","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/pycasbin.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-12-21T08:15:35.000Z","updated_at":"2024-07-16T15:47:15.000Z","dependencies_parsed_at":"2022-08-17T20:55:35.539Z","dependency_job_id":null,"html_url":"https://github.com/pycasbin/postgresql-watcher","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pycasbin%2Fpostgresql-watcher","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pycasbin%2Fpostgresql-watcher/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pycasbin%2Fpostgresql-watcher/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pycasbin%2Fpostgresql-watcher/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pycasbin","download_url":"https://codeload.github.com/pycasbin/postgresql-watcher/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229740054,"owners_count":18116882,"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":["casbin","postgres","postgresql","pycasbin","watcher"],"created_at":"2024-12-14T18:29:00.467Z","updated_at":"2024-12-14T18:29:01.015Z","avatar_url":"https://github.com/pycasbin.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# postgresql-watcher\n\n[![Build Status](https://github.com/pycasbin/postgresql-watcher/actions/workflows/release.yml/badge.svg)](https://github.com/pycasbin/postgresql-watcher/actions/workflows/release.yml)\n[![Coverage Status](https://coveralls.io/repos/github/pycasbin/postgresql-watcher/badge.svg)](https://coveralls.io/github/pycasbin/postgresql-watcher)\n[![Version](https://img.shields.io/pypi/v/casbin-postgresql-watcher.svg)](https://pypi.org/project/casbin-postgresql-watcher/)\n[![PyPI - Wheel](https://img.shields.io/pypi/wheel/casbin-postgresql-watcher.svg)](https://pypi.org/project/casbin-postgresql-watcher/)\n[![Pyversions](https://img.shields.io/pypi/pyversions/casbin-postgresql-watcher.svg)](https://pypi.org/project/casbin-postgresql-watcher/)\n[![Download](https://img.shields.io/pypi/dm/casbin-postgresql-watcher.svg)](https://pypi.org/project/casbin-postgresql-watcher/)\n[![Discord](https://img.shields.io/discord/1022748306096537660?logo=discord\u0026label=discord\u0026color=5865F2)](https://discord.gg/S5UjpzGZjN)\n\nCasbin watcher based on PostgreSQL for monitoring updates to casbin policies.\n\n## Installation\n```bash\npip install casbin-postgresql-watcher\n```\n\n## Basic Usage Example\n\n```python\nfrom flask_authz import CasbinEnforcer\nfrom postgresql_watcher import PostgresqlWatcher\nfrom flask import Flask\nfrom casbin.persist.adapters import FileAdapter\n\ncasbin_enforcer = CasbinEnforcer(app, adapter)\nwatcher = PostgresqlWatcher(host=HOST, port=PORT, user=USER, password=PASSWORD, dbname=DBNAME)\nwatcher.set_update_callback(casbin_enforcer.load_policy)\ncasbin_enforcer.set_watcher(watcher)\n\n# Call should_reload before every call of enforce to make sure\n# the policy is update to date\nwatcher.should_reload()\nif casbin_enforcer.enforce(\"alice\", \"data1\", \"read\"):\n    # permit alice to read data1\n    pass\nelse:\n    # deny the request, show an error\n    pass\n```\n\nalternatively, if you need more control\n\n```python\nfrom flask_authz import CasbinEnforcer\nfrom postgresql_watcher import PostgresqlWatcher\nfrom flask import Flask\nfrom casbin.persist.adapters import FileAdapter\n\ncasbin_enforcer = CasbinEnforcer(app, adapter)\nwatcher = PostgresqlWatcher(host=HOST, port=PORT, user=USER, password=PASSWORD, dbname=DBNAME)\ncasbin_enforcer.set_watcher(watcher)\n\n# Call should_reload before every call of enforce to make sure\n# the policy is update to date\nif watcher.should_reload():\n    casbin_enforcer.load_policy()\n\nif casbin_enforcer.enforce(\"alice\", \"data1\", \"read\"):\n    # permit alice to read data1\n    pass\nelse:\n    # deny the request, show an error\n    pass\n```\n\n## Basic Usage Example With SSL Enabled\n\nSee [PostgresQL documentation](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS) for full details of SSL parameters.\n\n```python\n...\nwatcher = PostgresqlWatcher(host=HOST, port=PORT, user=USER, password=PASSWORD, dbname=DBNAME, sslmode=\"verify_full\", sslcert=SSLCERT, sslrootcert=SSLROOTCERT, sslkey=SSLKEY)\n...\n```\n\n\n## Django setup with casbin django orm adapter\n\nEnforcer and Watcher setup\n```\n# settings.py\nBASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))\n\nINSTALLED_APPS += [\n    'casbin_adapter.apps.CasbinAdapterConfig',\n]\n\nCASBIN_MODEL = os.path.join(BASE_DIR, 'casbin.conf')\n\nfrom postgresql_watcher.watcher import PostgresqlWatcher\nfrom casbin_adapter.enforcer import enforcer\n\nwatcher = PostgresqlWatcher(host=BANK_CONNECT_APIS_PG_HOST_URL, port=BANK_CONNECT_APIS_PG_PORT,\n            user=BANK_CONNECT_APIS_PG_USER, password=BANK_CONNECT_APIS_PG_PASSWORD, dbname=BANK_CONNECT_APIS_PG_DBNAME)\n\ndef update_enforcer():\n    print(\"before loading policy\", enforcer)\n    enforcer.load_policy()\n\nwatcher.set_update_callback(update_enforcer)\nCASBIN_WATCHER = watcher\n```\n\nUsage of enforcer\n\n```\n#views.py or any other file\nfrom casbin_adapter.enforcer import enforcer\n\nroles = enforcer.get_filtered_named_grouping_policy(\"g\", 1, str(member_id))\n```\n\n### Reload Casbin enforcer\nIn current setup enforcer does not automatically refresh in memory data, we can call watcher.should_reload() before every data access from enforcer.\n```\nfrom setting import watcher \nwatcher.should_reload()\n```\nIf there are any changes in db this call will refresh in memory data from database\n\nFor automatic reloading of data, parent process need to poll child process for messages and call should_reload function if there is any message in pipe between child and parent process","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpycasbin%2Fpostgresql-watcher","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpycasbin%2Fpostgresql-watcher","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpycasbin%2Fpostgresql-watcher/lists"}