{"record":{"id":"b317a8689ee9204c","repo":"ory/hydra","slug":"s-s-s-when-using-flag-e-environment-variable","errorCode":null,"errorMessage":"%s\n%s\n%s\n When using flag -e, environment variable DSN must be set. When using flag -c, the dsn property should be set.","messagePattern":"(.+?)\n(.+?)\n(.+?)\n When using flag -e, environment variable DSN must be set\\. When using flag -c, the dsn property should be set\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/cli/handler_janitor.go","lineNumber":127,"sourceCode":"\t}\n\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)","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/ory/hydra/blob/4174065ffb052799890f7480f5360a877a67ffc1/cmd/cli/handler_janitor.go#L109-L145","documentation":"After the driver is created successfully, the janitor checks whether a DSN is configured. If d.Config().DSN() is empty, it returns a usage error (formatted with cmd.UsageString()) telling the user they must supply the DSN either via the -e flag/environment variable or the dsn property in the -c config file.","triggerScenarios":"Running `hydra janitor` without the -e flag, with DSN unset, and with a -c config file that lacks the dsn property — so no DSN is present anywhere.","commonSituations":"Running janitor in a shell where the DSN env var wasn't exported (e.g. only set in a .env file that wasn't sourced); CI jobs invoking janitor without injecting secrets; config file pointing to a different environment that omits dsn.","solutions":["Pass the DSN explicitly: `hydra janitor -e \"postgres://user:pass@host:5432/db?sslmode=disable\"`.","Export the DSN environment variable before running: `export DSN=... && hydra janitor`.","Add `dsn: postgres://...` to the YAML config passed with -c.","Run `hydra janitor` alone first — its output includes cmd.UsageString() showing the expected flags."],"exampleFix":"// before\nhydra janitor --read-from-env  # DSN never exported\n\n// after\nexport DSN=\"postgres://hydra:secret@localhost:5432/hydra?sslmode=disable\"\nhydra janitor --cleanup-grace-period 24h --cleanup-limit 100","handlingStrategy":"validation","validationCode":"if len(os.Getenv(\"DSN\")) == 0 {\n    return fmt.Errorf(\"DSN must be set (env var or -e flag) before running hydra janitor\")\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Export DSN in shell profiles or CI secret injection, not just in .env files.","Pass -e explicitly in scripts so DSN presence is obvious.","Keep dsn in the -c config file for environment-specific deployments.","Run janitor with `set -eu` in shell scripts to catch unset vars early."],"tags":["cli","configuration","dsn","janitor","missing-env-var"],"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"}