{"id":"c4bdb129ae7c27ed","repo":"go-redis/redis","slug":"redis-invalid-database-number-q","errorCode":null,"errorMessage":"redis: invalid database number: %q","messagePattern":"redis: invalid database number: %q","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"options.go","lineNumber":700,"sourceCode":"\nfunc setupTCPConn(u *url.URL) (*Options, error) {\n\to := &Options{Network: \"tcp\"}\n\n\to.Username, o.Password = getUserPassword(u)\n\n\th, p := getHostPortWithDefaults(u)\n\to.Addr = net.JoinHostPort(h, p)\n\n\tf := strings.FieldsFunc(u.Path, func(r rune) bool {\n\t\treturn r == '/'\n\t})\n\tswitch len(f) {\n\tcase 0:\n\t\to.DB = 0\n\tcase 1:\n\t\tvar err error\n\t\tif o.DB, err = strconv.Atoi(f[0]); err != nil {\n\t\t\treturn nil, fmt.Errorf(\"redis: invalid database number: %q\", f[0])\n\t\t}\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"redis: invalid URL path: %s\", u.Path)\n\t}\n\n\tif u.Scheme == \"rediss\" {\n\t\to.TLSConfig = &tls.Config{\n\t\t\tServerName: h,\n\t\t\tMinVersion: tls.VersionTLS12,\n\t\t}\n\t}\n\n\treturn setupConnParams(u, o)\n}\n\n// getHostPortWithDefaults is a helper function that splits the url into\n// a host and a port. If the host is missing, it defaults to localhost\n// and if the port is missing, it defaults to 6379.","sourceCodeStart":682,"sourceCodeEnd":718,"githubUrl":"https://github.com/go-redis/redis/blob/36d97525cd8076aed67cddf54778e9ea84550929/options.go#L682-L718","documentation":"Returned by setupTCPConn when the single path segment of a redis:// URL is not a base-10 integer. The path (e.g. /3) selects the logical database; if strconv.Atoi fails on it, this error names the offending segment.","triggerScenarios":"A URL like redis://host:6379/db0, redis://host:6379/abc, or redis://host:6379/3x where the path component after the first slash is non-numeric.","commonSituations":"Confusing the DB selector with a path label (putting 'db0' in the URL), trailing slashes or stray characters in the path, or copy-pasting a URL that worked with another convention.","solutions":["Use a plain integer for the DB: redis://host:6379/3.","Omit the path entirely to select DB 0, or set Options.DB after ParseURL.","Prefer the ?db=N query parameter if you want to keep the path empty."],"exampleFix":"// before\nopt, err := redis.ParseURL(\"redis://localhost:6379/db0\")\n// after\nopt, err := redis.ParseURL(\"redis://localhost:6379/0\")","handlingStrategy":"validation","validationCode":"func validDBPathSegment(seg string) bool { _, err := strconv.Atoi(seg); return err == nil }","typeGuard":"func parseDBPath(u *url.URL) (int, bool) {\n    segs := strings.FieldsFunc(u.Path, func(r rune) bool { return r == '/' })\n    if len(segs) != 1 { return 0, false }\n    db, err := strconv.Atoi(segs[0]); return db, err == nil\n}","tryCatchPattern":null,"preventionTips":["Use a bare integer for the DB path segment.","Omit the path entirely to default to DB 0.","Prefer ?db=N over the path form for clarity."],"tags":["config","url","validation"],"analyzedSha":"36d97525cd8076aed67cddf54778e9ea84550929","analyzedAt":"2026-08-06T01:08:27.376Z","schemaVersion":2}