{"library":"sqlalchemy-adapter","title":"SQLAlchemy Adapter for PyCasbin","type":"library","description":"SQLAlchemy Adapter for PyCasbin enables Casbin, a powerful authorization library, to store and load policy rules from any SQLAlchemy-supported relational database. This includes popular databases like PostgreSQL, MySQL, SQLite, Oracle, Microsoft SQL Server, Firebird, and Sybase. The library is actively maintained, currently at version 1.9.0, and has a frequent release cadence, with multiple feature and bugfix releases in late 2025.","language":"python","status":"active","last_verified":"Fri May 15","install":{"commands":["pip install sqlalchemy-adapter"],"cli":null},"imports":["from sqlalchemy_adapter import Adapter"],"auth":{"required":false,"env_vars":[]},"links":{"homepage":null,"github":"https://github.com/officialpycasbin/sqlalchemy-adapter","docs":null,"changelog":null,"pypi":"https://pypi.org/project/sqlalchemy-adapter/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null},"quickstart":{"code":"import os\nimport casbin\nimport tempfile\nfrom sqlalchemy_adapter import Adapter\n\n# Create a dummy model.conf for the Enforcer\nmodel_conf_content = '''\n[request_definition]\nr = sub, obj, act\n\n[policy_definition]\np = sub, obj, act\n\n[policy_effect]\ne = some(where (p.eft == allow))\n\n[matchers]\nm = r.sub == p.sub && r.obj == p.obj && r.act == p.act\n'''\n\n# Write the model content to a temporary file\nwith tempfile.NamedTemporaryFile(mode='w+', delete=False, suffix=\".conf\") as model_file:\n    model_file.write(model_conf_content)\n    model_path = model_file.name\n\n# Use an in-memory SQLite database for a simple, runnable example.\n# For persistent storage, provide a file path or a full database URL\n# e.g., 'sqlite:///test.db' or from an environment variable.\ndatabase_url = os.environ.get('SQLALCHEMY_DATABASE_URL', 'sqlite:///:memory:')\n\ntry:\n    # 1. Initialize the SQLAlchemy Adapter\n    # By default, create_table=True, so tables will be created if they don't exist.\n    adapter = Adapter(database_url)\n\n    # 2. Initialize Casbin Enforcer with the adapter\n    e = casbin.Enforcer(model_path, adapter)\n\n    # 3. Add policies\n    e.add_policy(\"alice\", \"data1\", \"read\")\n    e.add_policy(\"bob\", \"data2\", \"write\")\n\n    # 4. Save policies to the database (persists changes through the adapter)\n    e.save_policy()\n\n    print(\"Policies added and saved.\")\n\n    # 5. Enforce policies\n    print(f\"Alice can read data1: {e.enforce('alice', 'data1', 'read')}\") # Expected: True\n    print(f\"Bob can write data2: {e.enforce('bob', 'data2', 'write')}\") # Expected: True\n    print(f\"Alice can write data1: {e.enforce('alice', 'data1', 'write')}\") # Expected: False\n\n    # 6. Load policies (useful if Enforcer was re-initialized or policy was changed elsewhere)\n    e.load_policy()\n    print(f\"After reloading, Alice can read data1: {e.enforce('alice', 'data1', 'read')}\") # Expected: True\n\nexcept Exception as ex:\n    print(f\"An error occurred: {ex}\")\nfinally:\n    # Clean up the temporary model file\n    os.remove(model_path)\n\n","lang":"python","description":"This quickstart demonstrates how to initialize the `SQLAlchemyAdapter` and integrate it with `pycasbin.Enforcer`. It uses an in-memory SQLite database for a self-contained example. Policy rules are added, saved to the database via the adapter, and then enforced. Ensure you have `pycasbin` installed alongside `sqlalchemy-adapter`.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":{"tag":null,"tag_description":null,"last_tested":"2026-05-15","installed_version":"1.9.0","pypi_latest":"1.9.0","is_stale":false,"summary":{"python_range":"3.10–3.9","success_rate":100,"avg_install_s":3.7,"avg_import_s":0.95,"wheel_type":"wheel"},"results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"sqlalchemy-adapter","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.82,"mem_mb":22.3,"disk_size":"46.6M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"sqlalchemy-adapter","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":3.7,"import_time_s":0.61,"mem_mb":22.3,"disk_size":"45M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"sqlalchemy-adapter","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":1.14,"mem_mb":25.2,"disk_size":"52.5M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"sqlalchemy-adapter","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":3.5,"import_time_s":1.03,"mem_mb":25.2,"disk_size":"51M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"sqlalchemy-adapter","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":1.16,"mem_mb":24.8,"disk_size":"43.6M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"sqlalchemy-adapter","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":3.3,"import_time_s":1.15,"mem_mb":24.8,"disk_size":"42M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"sqlalchemy-adapter","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":1.1,"mem_mb":24.6,"disk_size":"43.1M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"sqlalchemy-adapter","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":3.5,"import_time_s":1.1,"mem_mb":24.6,"disk_size":"42M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"sqlalchemy-adapter","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.75,"mem_mb":22.4,"disk_size":"45.2M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"sqlalchemy-adapter","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":4.3,"import_time_s":0.68,"mem_mb":22.4,"disk_size":"44M"}]}}