{"record":{"id":"05ba21fed1d890ab","repo":"ory/kratos","slug":"required-config-value-dsn-was-not-set","errorCode":null,"errorMessage":"required config value \"dsn\" was not set","messagePattern":"required config value \"dsn\" was not set","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/cleanup/handler.go","lineNumber":45,"sourceCode":"\topts := []configx.OptionModifier{\n\t\tconfigx.WithFlags(cmd.Flags()),\n\t\tconfigx.SkipValidation(),\n\t}\n\n\tif !flagx.MustGetBool(cmd, \"read-from-env\") {\n\t\tif len(args) != 1 {\n\t\t\treturn errors.New(`expected to get the DSN as an argument, or the \"read-from-env\" flag`)\n\t\t}\n\t\topts = append(opts, configx.WithValue(config.ViperKeyDSN, args[0]))\n\t}\n\n\td, err := driver.NewWithoutInit(\n\t\tcmd.Context(),\n\t\tcmd.ErrOrStderr(),\n\t\tdriver.WithConfigOptions(opts...),\n\t)\n\tif len(d.Config().DSN(cmd.Context())) == 0 {\n\t\treturn errors.New(`required config value \"dsn\" was not set`)\n\t} else if err != nil {\n\t\treturn errors.Wrap(err, \"An error occurred initializing cleanup\")\n\t}\n\n\terr = d.Init(cmd.Context(), &contextx.Default{})\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"An error occurred initializing cleanup\")\n\t}\n\n\tkeepLast := flagx.MustGetDuration(cmd, \"keep-last\")\n\n\terr = d.Persister().CleanupDatabase(\n\t\tcmd.Context(),\n\t\td.Config().DatabaseCleanupSleepTables(cmd.Context()),\n\t\tkeepLast,\n\t\td.Config().DatabaseCleanupBatchSize(cmd.Context()))\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"An error occurred while cleaning up expired data\")","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/ory/kratos/blob/b86338da04a040247a07f46100a86dcfb3875909/cmd/cleanup/handler.go#L27-L63","documentation":"After initializing the driver with the collected config options, CleanupSQL checks `d.Config().DSN(ctx)`. If the resolved DSN is empty — meaning no DSN was provided via argument, env (read-from-env), or config file — the command aborts. This error fires when the DSN path was taken (flag or arg present) but the value still resolved to empty, or driver init produced no DSN.","triggerScenarios":"Calling `cleanup sql --read-from-env` when the DSN env var is unset/empty; passing an empty-string argument `cleanup sql \"\"`; a config file that lacks the dsn key while validation was skipped (SkipValidation is used in the opts).","commonSituations":"Container/CI environments where the DSN env var was not propagated to the cleanup job; typos in the env var name; empty secret mounted for the DSN.","solutions":["Verify the DSN environment variable is actually set and non-empty in the runtime environment","Pass the DSN explicitly as an argument instead of relying on env","Check the config file loaded by configx contains a non-empty dsn key","Inspect logs or echo the env in the job to confirm variable propagation"],"exampleFix":"// before (env var missing)\nDSN= cleanup sql --read-from-env\n// after\nexport DSN='postgres://user:pass@host:5432/db?sslmode=disable'\ncleanup sql --read-from-env","handlingStrategy":"validation","validationCode":"if os.Getenv(\"DSN\") == \"\" && dsnArg == \"\" {\n  return errors.New(\"DSN must be set via argument or env before running cleanup\")\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Assert DSN env presence in your deployment's init/entrypoint script","Avoid empty-string arguments; validate the DSN parses (e.g. with a postgres URL parse) before invoking","Keep DSN in one secret and reference it consistently across services"],"tags":["config","database","env"],"backgroundTag":"missing-required-config-field","analyzedSha":"b86338da04a040247a07f46100a86dcfb3875909","analyzedAt":"2026-09-07T15:58:15.934Z","contentChangedAt":"2026-09-07T15:58:15.934Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}