// DuckDB error reference

IO Error: The database file is locked by another process

Another process holds the write lock on the DuckDB file, so this process cannot open it read-write.

Exception
IO Error
Kategorie
IO and permission errors
Fehlermeldung
IO Error: Could not set lock on file "/data/db.duckdb": Conflicting lock is held by another process. See also https://duckdb.org/docs/current/connect/concurrency

Was es bedeutet

DuckDB allows many processes on one file only if all of them are read-only. The first read-write connection takes an exclusive lock for the lifetime of its process, and any second process that wants the file read-write fails with this error. Inside a single process several connections share one lock, so the error can only come from a second process.

The wording varies by version: recent builds name the holder, for example Conflicting lock is held in /usr/local/bin/duckdb (PID 4242) by user app, and older builds say Conflicting lock is held by another process. Both point at the same file and the same collision.

Häufige Ursachen

  • A second process opened the same file read-write: a CLI session, a notebook kernel, a background job or another instance of your app.
  • An orchestrator retry started a new run while the previous one still holds the file.
  • The file sits on a network share where file locking behaves differently, so who holds the lock is unclear.

Find the cause

-- Confirm which file this connection actually opened, and whether it is read-only:
SELECT database_name, path, readonly FROM duckdb_databases();
-- Then, in your shell (not in DuckDB), list the processes holding it:
-- lsof /data/db.duckdb

Wie du es behebst

  1. Find the holder with your operating system's tools and close it, then retry. On macOS and Linux, lsof lists the process that has the file open.

  2. When you only need to query, open read-only. This only works when no read-write process holds the file, so it does not coexist with a running writer:

    ATTACH '/data/db.duckdb' AS wh (READ_ONLY);  -- fails while a read-write process holds the file
  3. Route all writes through one process. DuckDB has no cross-process table or row locks, so the choices are one writer plus readers that open only after the writer exits, or everything read-only.

    SELECT database_name, path FROM duckdb_databases();
  4. Serialize background jobs against each other in the orchestrator so two runs cannot target the same file at once.

Other ways to handle it

If several processes must work against the same data at once, give each writer its own DuckDB file and keep the shared data in Parquet or CSV, which every process can read concurrently. A single DuckDB file with one writer and read-only readers is the supported shape.

Sicher reproduzieren

ATTACH 'lock_demo.duckdb' AS db;
-- Keep this session open. In a second session run the same line:
ATTACH 'lock_demo.duckdb' AS db;   -- IO Error: Could not set lock on file "lock_demo.duckdb": Conflicting lock is held by another process

Fragen

Can two processes read the same file at the same time?

Yes, as long as every one of them opens it read-only. The exclusive lock is for writing, not for reading. A read-only open fails while a read-write process holds the file.

I closed everything and it still says locked. Is the lock stale?

Operating systems release locks when a process dies, so a truly stale lock is rare. The usual leftover is a service, editor plugin or notebook kernel still running. Check with lsof before suspecting corruption.

Zugehörige Fehler

Der Code und der Meldungstext stammen aus der DuckDB documentation. Durchstöbere die vollständige DuckDB error list oder Jeden Datenbankfehler durchsuchen.

// weniger Fehler, schnellere Korrekturen

Work on DuckDB in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for DuckDB and the other databases you use.

Text und Daten auf dieser Seite: CC BY 4.0, Quelle: SQLly (sqlly.app). Code und SQL-Snippets: MIT. Lizenzdetails Zitierte Fehlermeldungstexte gehören dem jeweiligen Datenbankhersteller.