{"record":{"id":"0e1fc5227236e369","repo":"ory/hydra","slug":"when-using-flag-e-environment-variable-dsn-must","errorCode":null,"errorMessage":"When using flag -e, environment variable DSN must be set.\nWhen using flag -c, the dsn property should be set.","messagePattern":"When using flag -e, environment variable DSN must be set\\.\nWhen using flag -c, the dsn property should be set\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/cli/handler_janitor.go","lineNumber":128,"sourceCode":"\n\tif !flagx.MustGetBool(cmd, ReadFromEnv) && len(flagx.MustGetStringSlice(cmd, Config)) == 0 {\n\t\tco = append(co, configx.WithValue(config.KeyDSN, args[0]))\n\t}\n\n\tdo := append(dOpts,\n\t\tdriver.DisableValidation(),\n\t\tdriver.DisablePreloading(),\n\t\tdriver.WithConfigOptions(co...),\n\t)\n\n\td, err := driver.New(ctx, do...)\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"Could not create driver\")\n\t}\n\n\tif len(d.Config().DSN()) == 0 {\n\t\t//lint:ignore ST1005 formatted error string used in CLI output\n\t\treturn fmt.Errorf(\"%s\\n%s\\n%s\\n\", cmd.UsageString(),\n\t\t\t\"When using flag -e, environment variable DSN must be set.\",\n\t\t\t\"When using flag -c, the dsn property should be set.\")\n\t}\n\n\tp := d.Persister()\n\n\tlimit := flagx.MustGetInt(cmd, Limit)\n\tbatchSize := flagx.MustGetInt(cmd, BatchSize)\n\n\tvar routineFlags []string\n\n\tif flagx.MustGetBool(cmd, OnlyTokens) {\n\t\troutineFlags = append(routineFlags, OnlyTokens)\n\t}\n\n\tif flagx.MustGetBool(cmd, OnlyRequests) {\n\t\troutineFlags = append(routineFlags, OnlyRequests)\n\t}","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/ory/hydra/blob/4174065ffb052799890f7480f5360a877a67ffc1/cmd/cli/handler_janitor.go#L110-L146","documentation":"After building the persistence driver in the janitor's purge step, RunE checks d.Config().DSN(). If the DSN is still empty (even though flag validation passed), it returns this error explaining that with -e the DSN env var must be set and with -c the dsn config property must be present. Arg validation cannot see the actual env/config values, so this is the runtime fallback check.","triggerScenarios":"Running `hydra janitor -e` (or with -c) where the DSN environment variable is unset/empty, or the config file lacks the dsn key — driver creation succeeds but config yields an empty DSN.","commonSituations":"Kubernetes CronJob with -e but the DSN secret not mounted/injected; config file valid YAML but missing the dsn property; DSN defined under the wrong config key or a differently named env var; docker-compose env_file not applied.","solutions":["Export/set the DSN environment variable before running with -e (e.g. DSN=postgres://... in the CronJob env).","Add the `dsn: ...` property to the config file used with -c.","Verify secret mounting / env_file wiring in the container runtime (kubectl describe pod, docker inspect).","Run `hydra janitor <dsn>` positionally as a simple bypass for one-off runs."],"exampleFix":"// before (k8s CronJob)\nenv: []\nargs: [\"janitor\", \"-e\", \"--tokens\"]\n\n// after\nenv:\n  - name: DSN\n    valueFrom:\n      secretKeyRef: { name: hydra, key: dsn }\nargs: [\"janitor\", \"-e\", \"--tokens\"]","handlingStrategy":"validation","validationCode":"# Verify DSN is actually resolvable before launching with -e:\nif [ -z \"$DSN\" ]; then echo \"DSN env var missing for janitor -e\" >&2; exit 1; fi\n# Or with config: grep -q '^dsn:' hydra.yml || exit 1","typeGuard":null,"tryCatchPattern":"// Wrapping RunE-style invocation:\nif err := runJanitor(); err != nil {\n    if strings.Contains(err.Error(), \"DSN must be set\") {\n        log.Fatal(\"janitor DSN missing: set DSN env (with -e) or dsn property (with -c)\")\n    }\n    log.Fatal(err)\n}","preventionTips":["Mount DSN as a k8s secret and assert it exists at container startup.","Keep config-file dsn key and env var naming consistent across environments.","Smoke-test janitor manually after any deployment/secret change.","Prefer positional DSN or explicit env over relying on config file discovery."],"tags":["cli","configuration","janitor","dsn","env"],"backgroundTag":"missing-env-var","analyzedSha":"4174065ffb052799890f7480f5360a877a67ffc1","analyzedAt":"2026-09-03T14:52:41.581Z","contentChangedAt":"2026-09-03T14:52:41.581Z","schemaVersion":2},"datasetVersion":"2026-09-10T17:17:09.494Z"}