# 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.

Source: https://sqlly.app/errors/duckdb/io-lock
Last updated: 2026-11-30
License: Text and data [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/) (credit SQLly, sqlly.app); code and SQL snippets [MIT](https://opensource.org/license/mit). [Details](https://sqlly.app/license).
Note: Quoted error message text belongs to its database vendor.

- Exception: `IO Error`
- Category: IO and permission errors

Error message:

```text
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
```

## What it means

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.

## Common causes

- 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.

## How to fix it

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:

   ```sql
   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.

   ```sql
   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.

## Reproduce it safely

```sql
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
```

## Questions

### 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.

## Related errors

- [`IO Error`](https://sqlly.app/errors/duckdb/io-file-not-found): A file could not be opened
- [`SQLITE_BUSY`](https://sqlly.app/errors/sqlite/sqlite-busy): SQLite: database is locked
- [`55P03`](https://sqlly.app/errors/postgresql/55P03): PostgreSQL: lock not available

The code and message text are from the [DuckDB documentation](https://duckdb.org/docs/).
