{"record":{"id":"31dc981894bc18ee","repo":"crowdsecurity/crowdsec","slug":"failed-to-generate-db-connection-string-w","errorCode":null,"errorMessage":"failed to generate DB connection string: %w","messagePattern":"failed to generate DB connection string: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/database/database.go","lineNumber":100,"sourceCode":"\t\tif _, err = os.Stat(config.DbPath); os.IsNotExist(err) {\n\t\t\tf, err := os.OpenFile(config.DbPath, os.O_CREATE|os.O_RDWR, 0o600)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"failed to create SQLite database file %q: %w\", config.DbPath, err)\n\t\t\t}\n\n\t\t\tif err := f.Close(); err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"failed to create SQLite database file %q: %w\", config.DbPath, err)\n\t\t\t}\n\t\t}\n\t\t// Always try to set permissions to simplify a bit the code for windows (as the permissions set by OpenFile will be garbage)\n\t\tif err = setFilePerm(config.DbPath, 0o640); err != nil {\n\t\t\treturn nil, fmt.Errorf(\"unable to set perms on %s: %w\", config.DbPath, err)\n\t\t}\n\t}\n\n\tdbConnectionString, err := config.ConnectionString()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to generate DB connection string: %w\", err)\n\t}\n\n\tdrv, err := getEntDriver(typ, dia, dbConnectionString, config)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed opening connection to %s: %w\", config.Type, err)\n\t}\n\n\tclient = ent.NewClient(ent.Driver(drv), entOpt)\n\n\tif config.LogLevel >= log.DebugLevel {\n\t\tlogger.Debugf(\"Enabling request debug\")\n\n\t\tclient = client.Debug()\n\t}\n\n\tif err = client.Schema.Create(ctx, dropLegacyIndex(\"decisions\", \"decision_value\")); err != nil {\n\t\treturn nil, fmt.Errorf(\"failed creating schema resources: %w\", err)\n\t}","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/database/database.go#L82-L118","documentation":"NewClient wraps the error returned by config.ConnectionString() when it cannot build a valid database connection string for the configured DB type (sqlite/mysql/postgres/etc.). It indicates a configuration problem, not a network problem: the DSN could not be produced at all.","triggerScenarios":"Calling crowdsec.NewClient / database.NewClient with a DbConfig whose Type or required connection fields are missing or malformed, so config.ConnectionString() returns an error (e.g. unsupported db type, empty host/port for mysql).","commonSituations":"Typo in the db_config.type in crowdsec.yml (e.g. 'sqlite3' instead of 'sqlite'); missing user/password/host fields for mysql/postgres; partially migrated or hand-edited config files; invalid special characters or unparseable DSN parameters.","solutions":["Check db_config.type in your config is exactly one of the supported values (sqlite, mysql, postgres, ...).","For mysql/postgres, verify all required fields (host, port, user, password, db name) are set in the config.","Inspect the wrapped inner error (%w) for the exact reason from ConnectionString().","Regenerate a known-good config (cscli setup or a stock crowdsec.yml) and diff against yours."],"exampleFix":"// before\ndb_config:\n  type: sqlite3\n  db_path: /var/lib/crowdsec/data/crowdsec.db\n// after\ndb_config:\n  type: sqlite\n  db_path: /var/lib/crowdsec/data/crowdsec.db","handlingStrategy":"validation","validationCode":"cfg := config.DbConfig{Type: \"sqlite\", DbPath: \"/var/lib/crowdsec/data/crowdsec.db\"}\nif cfg.Type != \"sqlite\" && cfg.Type != \"mysql\" && cfg.Type != \"postgres\" {\n    return fmt.Errorf(\"unsupported db type: %q\", cfg.Type)\n}\nif _, err := cfg.ConnectionString(); err != nil {\n    return fmt.Errorf(\"db config invalid: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"client, err := database.NewClient(ctx, cfg)\nif err != nil {\n    if strings.Contains(err.Error(), \"failed to generate DB connection string\") {\n        return fmt.Errorf(\"check db_config settings: %w\", err)\n    }\n    return err\n}","preventionTips":["Keep db_config.type to documented values only","Generate config via cscli rather than hand-editing","Validate ConnectionString() early at startup","Diff against a stock crowdsec.yml after upgrades"],"tags":["database","configuration","crowdsec"],"backgroundTag":"invalid-config-value","analyzedSha":"909b5157986a2b2c2163300fdaef5ed01289f7d2","analyzedAt":"2026-09-06T12:27:26.012Z","contentChangedAt":"2026-09-06T12:27:26.012Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}