{"record":{"id":"e49d74f163896fdf","repo":"BerriAI/litellm","slug":"connect-proxy-to-database-to-generate-keys-https","errorCode":null,"errorMessage":"Connect Proxy to database to generate keys - https://docs.litellm.ai/docs/proxy/virtual-keys ","messagePattern":"Connect Proxy to database to generate keys - https://docs\\.litellm\\.ai/docs/proxy/virtual-keys ","errorType":"exception","errorClass":"Exception","httpStatus":500,"severity":"critical","filePath":"litellm/proxy/management_endpoints/key_management_endpoints.py","lineNumber":3805,"sourceCode":"    table_name: Literal[\"key\", \"user\"] | None = None,\n    send_invite_email: bool | None = None,\n    created_by: str | None = None,\n    updated_by: str | None = None,\n    allowed_routes: list | None = None,\n    key_type: str | None = None,\n    sso_user_id: str | None = None,\n    object_permission_id: str | None = None,  # object_permission_id <-> LiteLLM_ObjectPermissionTable\n    object_permission: LiteLLM_ObjectPermissionBase | None = None,\n    auto_rotate: bool | None = None,\n    rotation_interval: str | None = None,\n    router_settings: dict | None = None,\n    access_group_ids: list[str] | None = None,\n    budget_limits: list | None = None,  # multiple concurrent budget windows\n):\n    from litellm.proxy.proxy_server import premium_user, prisma_client\n\n    if prisma_client is None:\n        raise Exception(\"Connect Proxy to database to generate keys - https://docs.litellm.ai/docs/proxy/virtual_keys \")\n\n    if token is None:\n        if key is not None:\n            token = key\n        else:\n            token = f\"sk-{secrets.token_urlsafe(LENGTH_OF_LITELLM_GENERATED_KEY)}\"\n\n    if duration is None:  # allow tokens that never expire\n        expires = None\n    else:\n        # Add duration to current time for exact expiration (not standardized reset time)\n        duration_seconds: Final = duration_in_seconds(duration)\n        expires = datetime.now(timezone.utc) + timedelta(seconds=duration_seconds)\n\n    if key_budget_duration is None:  # one-time budget\n        key_reset_at = None\n    else:\n        key_reset_at = get_budget_reset_time(budget_duration=key_budget_duration)","sourceCodeStart":3787,"sourceCodeEnd":3823,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/proxy/management_endpoints/key_management_endpoints.py#L3787-L3823","documentation":"generate_key_helper_fn is the core behind POST /key/generate and /user/new; virtual keys are DB records, so it immediately raises this Exception when prisma_client is None. If you see it, the proxy has no database attached, making all key generation impossible (the UI's 'Create Key' button fails the same way).","triggerScenarios":"POST /key/generate (or any flow that creates a key, including SSO user auto-provisioning) on a proxy started without DATABASE_URL / database config; DB env var lost in a container restart or redeploy.","commonSituations":"Fresh local install without Postgres; UI enabled but DB never wired; DATABASE_URL present in shell but proxy run under systemd/k8s with a sanitized env; migration to a new orchestrator dropped the secret.","solutions":["Set DATABASE_URL to a Postgres connection string (or add database_connection.url in the config) and restart the proxy.","Verify the DB is reachable from the proxy and Prisma migrations applied (proxy logs 'Prisma DB Connected' on startup).","Add a startup/health assertion on DB connectivity in your deployment before enabling key issuance."],"exampleFix":"# before\n# no database configured\nclient.post(\"/key/generate\", json={\"models\": [\"gpt-4o\"]})\n\n# after\nexport DATABASE_URL=\"postgresql://user:pass@db:5432/litellm\"\n# restart proxy, then:\nclient.post(\"/key/generate\", json={\"models\": [\"gpt-4o\"]})","handlingStrategy":"validation","validationCode":"if not os.environ.get(\"DATABASE_URL\"):\n    raise RuntimeError(\"key generation requires a Postgres DB: set DATABASE_URL before starting litellm\")\nkey = client.post(\"/key/generate\", json={\"models\": [\"gpt-4o\"]}).json()","typeGuard":null,"tryCatchPattern":"try:\n    key = client.post(\"/key/generate\", json={\"models\": [\"gpt-4o\"]})\n    key.raise_for_status()\nexcept HTTPError as e:\n    if \"Connect Proxy to database\" in e.response.text:\n        raise RuntimeError(\"litellm has no DB attached; provisioning blocked\") from e\n    raise","preventionTips":["Make a Postgres database a hard prerequisite in every environment that issues virtual keys.","Fail deployments fast: assert DB connectivity (and 'Prisma DB Connected' in logs) before enabling the UI/API.","Keep DB migrations and DATABASE_URL in infrastructure-as-code so restarts and redeploys stay consistent."],"tags":["database","connection","key-generation","litellm-proxy","config"],"backgroundTag":"database-not-connected","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","schemaVersion":2},"datasetVersion":"2026-08-21T13:17:26.733Z"}