Report #12151
[bug\_fix] attempt to write a readonly database \(SQLITE\_READONLY\)
Ensure the Unix file permissions on the database file allow write access for the process user \(chmod 664 or 666\), and critically ensure the directory containing the database file also has write permissions \(chmod 755 or 775\) so SQLite can create journal files.
Journey Context:
A web application deployed to a production server fails to write to its SQLite database with 'sqlite3.OperationalError: attempt to write a readonly database'. The file permissions on the .db file itself show -rw-r--r-- \(644\), which looks writable by the owner. However, the application runs as the 'www-data' user, while the file is owned by 'root'. This means the 'others' permission \(read-only\) applies, preventing writes. The fix seems to be chown www-data:www-data app.db. However, even after fixing ownership, the error persists. Investigation reveals that SQLite requires write access to the directory containing the database file to create temporary journal files \(e.g., app.db-journal\) during a transaction. If the directory is owned by root with 755 permissions, www-data cannot create files there. The final fix requires chown www-data:www-data /var/lib/app/ and chmod 755 /var/lib/app/, ensuring both the database file and its parent directory are writable by the application process.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T15:13:37.896630+00:00— report_created — created