dgraph-io/badger · error
Cannot find directory %q for read-only open
Error message
Cannot find directory %q for read-only open
What it means
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.
Source
Thrown at db.go:2074
}
}
func (db *DB) syncDir(dir string) error {
if db.opt.InMemory {
return nil
}
return syncDir(dir)
}
func createDirs(opt Options) error {
for _, path := range []string{opt.Dir, opt.ValueDir} {
dirExists, err := exists(path)
if err != nil {
return y.Wrapf(err, "Invalid Dir: %q", path)
}
if !dirExists {
if opt.ReadOnly {
return fmt.Errorf("Cannot find directory %q for read-only open", path)
}
// Try to create the directory
err = os.MkdirAll(path, 0700)
if err != nil {
return y.Wrapf(err, "Error Creating Dir: %q", path)
}
}
}
return nil
}
// Stream the contents of this DB to a new DB with options outOptions that will be
// created in outDir.
func (db *DB) StreamDB(outOptions Options) error {
outDir := outOptions.Dir
// Open output DB.
outDB, err := OpenManaged(outOptions)View on GitHub (pinned to 2a001d466f)
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
Example fix
// before
db, err := badger.Open(badger.DefaultOptions(dir).WithReadOnly(true))
// after
if err := os.MkdirAll(dir, 0700); err != nil { return err }
db, err := badger.Open(badger.DefaultOptions(dir).WithReadOnly(true)) Defensive patterns
Strategy: validation
Validate before calling
if info, err := os.Stat(dir); os.IsNotExist(err) {
return fmt.Errorf("badger dir %q does not exist for read-only open", dir)
} Try / catch
db, err := badger.Open(opts)
if err != nil && strings.Contains(err.Error(), "Cannot find directory") {
// fail fast with clear config guidance
return fmt.Errorf("check badger Dir/ValueDir %q and volume mounts: %w", opts.Dir, err)
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
AI-assisted analysis of dgraph-io/badger@2a001d466f (2026-09-05).
Data as JSON: /api/errors/c7eb6de40a3b925f.
Report an issue: GitHub.