crowdsecurity/crowdsec · error
failed to generate DB connection string: %w
Error message
failed to generate DB connection string: %w
What it means
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.
Source
Thrown at pkg/database/database.go:100
if _, err = os.Stat(config.DbPath); os.IsNotExist(err) {
f, err := os.OpenFile(config.DbPath, os.O_CREATE|os.O_RDWR, 0o600)
if err != nil {
return nil, fmt.Errorf("failed to create SQLite database file %q: %w", config.DbPath, err)
}
if err := f.Close(); err != nil {
return nil, fmt.Errorf("failed to create SQLite database file %q: %w", config.DbPath, err)
}
}
// Always try to set permissions to simplify a bit the code for windows (as the permissions set by OpenFile will be garbage)
if err = setFilePerm(config.DbPath, 0o640); err != nil {
return nil, fmt.Errorf("unable to set perms on %s: %w", config.DbPath, err)
}
}
dbConnectionString, err := config.ConnectionString()
if err != nil {
return nil, fmt.Errorf("failed to generate DB connection string: %w", err)
}
drv, err := getEntDriver(typ, dia, dbConnectionString, config)
if err != nil {
return nil, fmt.Errorf("failed opening connection to %s: %w", config.Type, err)
}
client = ent.NewClient(ent.Driver(drv), entOpt)
if config.LogLevel >= log.DebugLevel {
logger.Debugf("Enabling request debug")
client = client.Debug()
}
if err = client.Schema.Create(ctx, dropLegacyIndex("decisions", "decision_value")); err != nil {
return nil, fmt.Errorf("failed creating schema resources: %w", err)
}View on GitHub (pinned to 909b515798)
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.
Example fix
// before db_config: type: sqlite3 db_path: /var/lib/crowdsec/data/crowdsec.db // after db_config: type: sqlite db_path: /var/lib/crowdsec/data/crowdsec.db
Defensive patterns
Strategy: validation
Validate before calling
cfg := config.DbConfig{Type: "sqlite", DbPath: "/var/lib/crowdsec/data/crowdsec.db"}
if cfg.Type != "sqlite" && cfg.Type != "mysql" && cfg.Type != "postgres" {
return fmt.Errorf("unsupported db type: %q", cfg.Type)
}
if _, err := cfg.ConnectionString(); err != nil {
return fmt.Errorf("db config invalid: %w", err)
} Try / catch
client, err := database.NewClient(ctx, cfg)
if err != nil {
if strings.Contains(err.Error(), "failed to generate DB connection string") {
return fmt.Errorf("check db_config settings: %w", err)
}
return err
} Prevention
- 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
When it happens
Trigger: 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).
Common situations: 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.
Understand the failure class
Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.
Related errors
- no database configuration provided
- unable to update
- unable to delete
- object not found
- unable to parse time
AI-assisted analysis of crowdsecurity/crowdsec@909b515798 (2026-09-06).
Data as JSON: /api/errors/31dc981894bc18ee.
Report an issue: GitHub.