The log line is attempt to write a readonly database. The file mode looks fine, or the SELECT you just ran returned rows, and the next statement dies.
SQLite uses that one sentence for the primary code SQLITE_READONLY (8) and for the extended codes under it. I hit two of them on SQLite 3.53.4, as a user that was not root (uid 65534), from Python 3.12. Root is a bad test here: it ignores the mode bits, and a 0444 file still accepted an INSERT.
The rules for database is locked are in
SQLite locking. The WAL snapshot case, where busy_timeout never waits, is in
SQLITE_BUSY in WAL mode. This one is permission, not a lock.
Short version:
- Code 8 means this connection may not change the database. A file mode of
0444, a URI opened withmode=ro, andPRAGMA query_only=ONall came back as 8. - Code 1544 (
SQLITE_READONLY_DIRECTORY) means SQLite cannot create a journal file next to the database. On a WAL database whose-waland-shmwere already gone, a directory mode of0555failed theSELECTtoo. - If
-waland-shmalready exist and are writable, a directory you cannot create files in can still accept anINSERT. The folder has to be writable when those files are created, not for every later write.
The sentence hides the code
Python’s sqlite3.OperationalError prints the sentence. The number is on the exception, and only if you ask.
import sqlite3
con = sqlite3.connect("app.db")
try:
con.execute("INSERT INTO t(x) VALUES (2)")
except sqlite3.OperationalError as e:
print(e.sqlite_errorcode, e.sqlite_errorname)
On Python 3.11 and later, sqlite_errorcode is the extended code. 8 and 1544 both print attempt to write a readonly database. The result code list is where the names are defined. The low 8 bits are the primary code, so 1544 & 0xff is still 8.
| Code | Name | What it meant in this run, or in the docs |
|---|---|---|
| 8 | SQLITE_READONLY | The connection has no write permission on the data. File mode 0444, mode=ro, and query_only=ON all returned 8 |
| 1544 | SQLITE_READONLY_DIRECTORY | Cannot create a journal file in the database’s directory. A WAL file with no sidecars, in a 0555 directory, returned 1544 on SELECT |
| 264 | SQLITE_READONLY_RECOVERY | Documented: a WAL database needs recovery, and recovery needs a write, but the connection is read-only. Not hit in this run |
| 776 | SQLITE_READONLY_ROLLBACK | Documented: a hot rollback journal needs to be rolled back, and the database is read-only. Not hit in this run |
SQLITE_READONLY_CANTINIT (1288) is the shared-memory case. The docs say higher-level code usually catches it and builds a temporary shared-memory region, so the application should not see 1288.
Three ways to get code 8
The database was one table, one row, journal mode left at the default. The process was uid 65534.
A file mode of 0444 still returned the row. The INSERT failed with code 8.
os.chmod("ro.db", 0o444)
con = sqlite3.connect("ro.db")
list(con.execute("SELECT * FROM t")) # [(1,)]
con.execute("INSERT INTO t(x) VALUES (2)") # code 8
Opening the same file with the URI file:ro.db?mode=ro failed the INSERT with code 8 as well. So did a normal read-write connection after PRAGMA query_only=ON. The file mode was 0644 for that last one. Nothing on disk was read-only. The connection was.
mode=ro and query_only are useful. They are also easy to leave on in a process that later writes. The error text will not mention either of them.
The folder, and the two extra files
WAL mode keeps commits in a -wal file and coordinates readers through a -shm file, both in the same directory as the database. The WAL document describes those files. When the last connection closes and the log has been checkpointed, SQLite deletes them. After both of my connections closed, the directory held only a.db.
I then set that directory to 0555 and opened the database again.
os.chmod("wald", 0o555)
con = sqlite3.connect("wald/a.db")
list(con.execute("SELECT * FROM t")) # code 1544, SQLITE_READONLY_DIRECTORY
The read failed. SQLite wanted to create a file beside a.db and could not. The message was still attempt to write a readonly database.
The other shape is the one that makes the advice “make the folder writable” look wrong. I left a connection open so a.db-wal and a.db-shm stayed on disk, committed a row into the log, then set the directory to 0555. The new connection read that row. The INSERT committed. Writing an existing file does not need permission to create files in the directory. Creating -wal and -shm does.
A container that mounts the database file alone puts the sidecars in the directory inside the container. A user who cannot write that directory gets 1544. Root gets no error, and the journal or -wal file lands in the container layer, apart from the database it belongs to, where it is lost with the container. Mount the directory.
Production settings for WAL, the one-writer connection, and checkpoints are in SQLite for a busy site.
What I would check, in this order
Look at sqlite_errorcode before changing modes. An 8 with a 0444 file is the file. With a 0644 file, check who owns it: a 0644 file owned by root also returned 8 to uid 65534. A 0444 -wal or -shm returned 8 too, on an ordinary connection. A 1544 is the directory: look for whether -wal and -shm, or the -journal in the default mode, can be created there. In the default mode a 0555 directory let the SELECT through and failed the INSERT with 1544.
If the code is 8 and the process can write all three files, I would print the URI it opened and the result of PRAGMA query_only.
Questions people ask
Why does the read fail when I only took write permission off the folder?
On the WAL database with no sidecar files, the SELECT was the statement that returned 1544. Opening that database wanted a file created next to it. A SELECT against the 0444 file, where the file itself was the thing I had made read-only, still returned the row.
Does busy_timeout help?
No. This is not a lock held by another connection. The timeout is for SQLITE_BUSY. The fix is the permission the code names, or a connection opened for writing.