Report #10040
[bug\_fix] SQLite attempt to write a readonly database
Root cause: This error \(SQLITE\_READONLY\) occurs when SQLite cannot acquire a write lock on the database file. Common causes include: \(1\) The database file or containing directory has incorrect Unix permissions \(e.g., owned by root but app runs as www-data\); \(2\) The database URL uses mode=ro or query parameter immutable=1; \(3\) The database is in WAL mode but the -wal or -shm files have different permissions than the main .db file, preventing coordinated write access; \(4\) The database is on a read-only filesystem or network share. The fix is to ensure the application user owns the .db file, the containing directory \(for lock files\), and any existing -wal/-shm files, with permissions 644/755. If using WAL mode \(recommended\), ensure it was enabled by the same user that runs the app.
Journey Context:
You deploy a Python Flask app using SQLite to a production Ubuntu server with Gunicorn. Locally it works fine, but in production every POST request fails with 'sqlite3.OperationalError: attempt to write a readonly database'. You check ls -l and the app.db file shows -rw-r--r-- 1 root root, but Gunicorn runs as www-data user. You chown www-data:www-data app.db but the error persists. You realize SQLite also needs write access to the directory to create lock files, and since you enabled WAL mode locally, there are app.db-wal and app.db-shm files still owned by root. You run chown -R www-data:www-data . and chmod 755 . in the data directory. The application now writes successfully. The root cause was the -wal and -shm file ownership mismatch in WAL mode combined with directory write permissions.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T09:43:11.125961+00:00— report_created — created