// SQLite error reference

SQLITE_READONLY: attempt to write a readonly database

The connection can't write: the file or its directory isn't writable, the database was opened read-only, or SQLite can't create its journal file.

Result code
SQLITE_READONLY
Name
8
Category
Primary result codes
Error message
attempt to write a readonly database

What it means

SQLITE_READONLY (8) is returned when a statement tries to change data the connection has no write permission for. Writing needs permission on the database file and also on its directory, because SQLite creates a journal (-journal) or WAL (-wal, -shm) file beside it.

Extended codes say why: SQLITE_READONLY_DIRECTORY (no permission to create the journal), SQLITE_READONLY_DBMOVED (the file was moved or deleted while open), SQLITE_READONLY_ROLLBACK (a hot journal needs rolling back but the database is read-only), and others.

Common causes

  • The database file is owned by another user or mounted read-only, typical for a web server user or a file inside an app bundle.
  • The directory isn't writable, so SQLite can't create the journal file even though the file itself is.
  • The connection was opened read-only (SQLITE_OPEN_READONLY, ?mode=ro URI, or PRAGMA query_only = ON).
  • The file was moved, renamed or deleted while the connection was open (SQLITE_READONLY_DBMOVED).

How to fix it

  1. Give the process write permission on the file and on its directory. On Linux and macOS check with ls -l on both, then fix ownership or mode.

  2. Check whether the connection itself is read-only.

    PRAGMA query_only;
    PRAGMA database_list;
  3. Open the database read-write: drop mode=ro from the URI or the read-only flag from the open call.

  4. If the file was moved while open, close the connection and open it again at its new path.

Related errors

More primary result codes errors

The code and message text are from the SQLite result and error codes documentation. Browse the full SQLite error list or search every database error.

// fewer errors, faster fixes

Work on SQLite in SQLly

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