Get Started Free

Fix "attempt to write a readonly database" in SQLite

Why SQLite says attempt to write a readonly database: file or folder permissions, a read-only open mode, or a file replaced under an open connection.

Updated November 26, 2026 ยท Tested on SQLite 3.43.2 (sqlite3 CLI) on macOS, 3.45.3 (Python 3.12 sqlite3), 3.49.2 (better-sqlite3 11.10)

Quick Answer

The process can read the database but can't write to it. Check that the user running your app can write both the database file and the folder it's in, because SQLite creates its journal file next to the database:

ls -l app.db           # file must be writable by the app's user
ls -ld "$(dirname app.db)"   # so must the folder
chmod u+w app.db && chmod u+w "$(dirname app.db)"

If the permissions are fine, the database was opened read-only (mode=ro, -readonly, { readonly: true }), or the file was replaced while a connection had it open.

What the error means

SQLite opened the database without write access and then a statement tried to change it. It returns SQLITE_READONLY (code 8). Reads still work, so the app often fails only on its first save:

ClientWhat you see
sqlite3 CLIError: stepping, attempt to write a readonly database (8)
Python sqlite3sqlite3.OperationalError: attempt to write a readonly database, SQLITE_READONLY
Node better-sqlite3SqliteError: attempt to write a readonly database, err.code === "SQLITE_READONLY"

What we found testing this

  • A read-only folder breaks writes even when the file is writable. With app.db at mode 644 inside a folder at 555, INSERT failed with "attempt to write a readonly database": SQLite couldn't create app.db-journal next to it. SELECT kept working.
  • A read-only file gives the same message (mode 444), with exit status 8 from the CLI.
  • Opening read-only on purpose looks identical. sqlite3.connect("file:app.db?mode=ro", uri=True), the CLI's -readonly flag and better-sqlite3's { readonly: true } all produced the same error on write, with no hint that it was the open mode.
  • Replacing the file under an open connection. Deleting m.db and creating a new empty file at the same path made the old connection's next write fail with the extended code SQLITE_READONLY_DBMOVED. This is what happens when a deploy or sync tool swaps the database while the app is running.

Cause 1: file or folder permissions

The app often runs as a different user from the one that created the file: a web server user, a service account, or root inside a container. Give that user write access to the folder and the file:

# example: the app runs as www-data
sudo chown -R www-data:www-data /var/lib/myapp
sudo chmod 755 /var/lib/myapp && sudo chmod 644 /var/lib/myapp/app.db

In WAL mode SQLite also writes app.db-wal and app.db-shm, which need the same owner. If they were created by another user (for example after running a migration with sudo), fix their ownership too.

In Docker, a database on a bind mount keeps the host's ownership. Either run the container as a user that owns the folder, or chown the folder to the container's user ID.

Cause 2: the database was opened read-only

Check how the connection is opened:

# read-only:   sqlite3.connect("file:app.db?mode=ro", uri=True)
# read-write:  sqlite3.connect("file:app.db?mode=rw", uri=True)   # fails if the file is missing
conn = sqlite3.connect("file:app.db?mode=rw", uri=True)
const db = new Database('app.db', { readonly: false, fileMustExist: true })

Files inside an app bundle, a read-only Docker layer, a mounted ISO or a :ro volume are always read-only. Copy the database to a writable location (the user's data folder, a named volume) on first run.

Cause 3: the file was moved or replaced

If the database is swapped out while the app runs (a deploy that copies a fresh .db, a restore, a sync client), existing connections still point at the old file and every write fails with SQLITE_READONLY_DBMOVED. Restart the app or reopen its connections after replacing the file. To update a live database safely, write into it with SQL or use the backup API instead of copying over it.

In VisuaLeaf

Check the file in VisuaLeaf

To rule out the database itself, open the same .db file in VisuaLeaf. You pick the file directly, with no connection string, and the SQL editor runs PRAGMA statements, so PRAGMA journal_mode; tells you whether the app also needs write access to -wal and -shm files. If you can edit rows there but your app can't, the problem is the app's user or open mode, not the file. See VisuaLeaf for SQLite.

Frequently Asked Questions

What causes "attempt to write a readonly database" in SQLite?

The process opened the database without write access. Usually the app's user cannot write the file or the folder that contains it, or the database was opened with mode=ro or a readonly option.

Why does SQLite need write access to the folder?

SQLite creates a journal file (app.db-journal, or app.db-wal and app.db-shm in WAL mode) in the same folder as the database while it writes. If it cannot create that file, the write fails as read-only.

How do I open a SQLite database read-only on purpose?

Use a URI with mode=ro, for example sqlite3.connect("file:app.db?mode=ro", uri=True) in Python, the -readonly flag in the sqlite3 shell, or { readonly: true } in better-sqlite3.

What is SQLITE_READONLY_DBMOVED?

The database file was deleted, renamed or replaced while a connection still had it open. Reopen the connection after replacing the file.

Free Tools

VisuaLeaf for SQLite

Open any SQLite file and check it yourself

VisuaLeaf opens .db and .sqlite files directly and runs SQL and PRAGMA statements against them, so you can tell a file problem from an app problem in seconds.