{"record":{"id":"d098161bdf7de7c6","repo":"ory/hydra","slug":"could-not-create-driver","errorCode":null,"errorMessage":"Could not create driver","messagePattern":"Could not create driver","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/cli/handler_janitor.go","lineNumber":123,"sourceCode":"\tnotAfter := time.Now()\n\n\tif keepYounger := flagx.MustGetDuration(cmd, KeepIfYounger); keepYounger > 0 {\n\t\tnotAfter = notAfter.Add(-keepYounger)\n\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)","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/ory/hydra/blob/4174065ffb052799890f7480f5360a877a67ffc1/cmd/cli/handler_janitor.go#L105-L141","documentation":"The Hydra janitor CLI constructs a SQL driver from the -e/--environment (DSN) or -c/--config flags and calls driver.New. This wrapped error is returned when driver.New fails, meaning the persister could not be initialized — almost always a bad or unreachable DSN or an unsupported/missing driver connection scheme.","triggerScenarios":"Running `hydra janitor` where driver.New(ctx, do...) fails: malformed DSN, unsupported scheme (missing postgres/mysql driver registration), or invalid driver config options passed via WithConfigOptions.","commonSituations":"DSN env var pointing at a database that is down or unreachable; DSN missing the connection scheme (e.g. 'postgres://' prefix dropped); running janitor with no -e flag so DSN is empty (see the follow-up usage error); wrong credentials so the driver fails its connection check.","solutions":["Inspect the wrapped cause printed below this message (errors.Wrap preserves the original driver.New error) and fix the underlying connection problem.","Verify the DSN set via -e flag or DSN environment variable is well-formed and reachable: `hydra janitor -e \"postgres://user:pass@host:5432/db?sslmode=disable\"`.","Confirm the database is running and network-reachable from where the CLI runs (docker network, VPN, security groups).","Test the same DSN with `hydra serve` or psql/mysql client to isolate janitor-specific config issues."],"exampleFix":"// before\nhydra janitor -c bad-config.yml\n\n// after\nDSN=\"postgres://hydra:secret@localhost:5432/hydra?sslmode=disable\" hydra janitor -e \"$DSN\" --cleanup-grace-period 24h","handlingStrategy":"try-catch","validationCode":"if os.Getenv(\"DSN\") == \"\" && config.DSN == \"\" {\n    return fmt.Errorf(\"janitor requires a DSN via -e flag or DSN env var\")\n}","typeGuard":null,"tryCatchPattern":"if err := cmd.Run(); err != nil {\n    var uerr *usageError\n    if errors.As(err, &uerr) {\n        fmt.Fprintln(os.Stderr, uerr.Usage())\n    }\n    log.Fatalf(\"janitor failed: %+v\", err) // print full stack to see wrapped driver.New cause\n}","preventionTips":["Validate the DSN with a connectivity check (ping/SELECT 1) before running janitor in CI.","Keep the DSN scheme prefix intact (postgres://, mysql://).","Source .env files before invoking the CLI.","Test the DSN with `hydra serve` or a native DB client to isolate network issues."],"tags":["cli","database","driver","janitor","configuration"],"backgroundTag":"database-connection-failed","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"}