{"record":{"id":"c7eb6de40a3b925f","repo":"dgraph-io/badger","slug":"cannot-find-directory-q-for-read-only-open","errorCode":null,"errorMessage":"Cannot find directory %q for read-only open","messagePattern":"Cannot find directory %q for read-only open","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"db.go","lineNumber":2074,"sourceCode":"\t}\n}\n\nfunc (db *DB) syncDir(dir string) error {\n\tif db.opt.InMemory {\n\t\treturn nil\n\t}\n\treturn syncDir(dir)\n}\n\nfunc createDirs(opt Options) error {\n\tfor _, path := range []string{opt.Dir, opt.ValueDir} {\n\t\tdirExists, err := exists(path)\n\t\tif err != nil {\n\t\t\treturn y.Wrapf(err, \"Invalid Dir: %q\", path)\n\t\t}\n\t\tif !dirExists {\n\t\t\tif opt.ReadOnly {\n\t\t\t\treturn fmt.Errorf(\"Cannot find directory %q for read-only open\", path)\n\t\t\t}\n\t\t\t// Try to create the directory\n\t\t\terr = os.MkdirAll(path, 0700)\n\t\t\tif err != nil {\n\t\t\t\treturn y.Wrapf(err, \"Error Creating Dir: %q\", path)\n\t\t\t}\n\t\t}\n\t}\n\treturn nil\n}\n\n// Stream the contents of this DB to a new DB with options outOptions that will be\n// created in outDir.\nfunc (db *DB) StreamDB(outOptions Options) error {\n\toutDir := outOptions.Dir\n\n\t// Open output DB.\n\toutDB, err := OpenManaged(outOptions)","sourceCodeStart":2056,"sourceCodeEnd":2092,"githubUrl":"https://github.com/dgraph-io/badger/blob/2a001d466f6b71a917319a1db41f99860e16e269/db.go#L2056-L2092","documentation":"Returned by badger.Open/OpenDB when the configured directory does not exist and the options request read-only mode. In normal mode badger creates the directory (os.MkdirAll 0700), but a read-only open must not create anything, so it fails with this error instead.","triggerScenarios":"Calling badger.Open (or OpenBanned/standby variants) with opt.ReadOnly = true and a Dir (or ValueDir) path that does not exist on disk — including typos, unmounted volumes, or a path that exists only after app startup creates it.","commonSituations":"Typo in the DB path config; mounting the data volume after the app starts; Kubernetes/container where the volume mount is missing; running a read-replica against a directory that was never created; Docker volume not mounted into the container.","solutions":["Create the directory before opening: os.MkdirAll(dir, 0700) then open with ReadOnly: true","Verify the Dir and ValueDir paths in your options are correct and the volume is mounted","If you control the code path, drop ReadOnly (or create the dir) when the directory is absent"],"exampleFix":"// before\ndb, err := badger.Open(badger.DefaultOptions(dir).WithReadOnly(true))\n\n// after\nif err := os.MkdirAll(dir, 0700); err != nil { return err }\ndb, err := badger.Open(badger.DefaultOptions(dir).WithReadOnly(true))","handlingStrategy":"validation","validationCode":"if info, err := os.Stat(dir); os.IsNotExist(err) {\n    return fmt.Errorf(\"badger dir %q does not exist for read-only open\", dir)\n}","typeGuard":null,"tryCatchPattern":"db, err := badger.Open(opts)\nif err != nil && strings.Contains(err.Error(), \"Cannot find directory\") {\n    // fail fast with clear config guidance\n    return fmt.Errorf(\"check badger Dir/ValueDir %q and volume mounts: %w\", opts.Dir, err)\n}","preventionTips":["Ensure the data volume is mounted before opening the DB","Validate Dir and ValueDir paths at config load time","Create the directory (0700) ahead of a read-only open","Use absolute paths to avoid cwd-relative surprises"],"tags":["badger","read-only","filesystem","missing-directory"],"backgroundTag":"directory-not-found","analyzedSha":"2a001d466f6b71a917319a1db41f99860e16e269","analyzedAt":"2026-09-05T13:00:02.264Z","contentChangedAt":"2026-09-05T13:00:02.264Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}