Skip to content
Databases3 min read

SQLite error: attempt to write a readonly database

attempt to write a readonly database

Short answer

`attempt to write a readonly database` means SQLite opened the file read-only because the app's user can't write to the database file or to the folder that holds it (SQLite writes a temporary file next to it). Make the file `644` and the folder `755`, owned by your account, and keep the database at an absolute path outside `public_html` that deploys don't touch.

ArabyHost teamLast updated: ShareWhatsAppXLinkedIn
Contents
  1. The most likely causes, in order
  2. How to confirm it’s fixed
  3. Prevention
  4. On ArabyHost

The app runs and shows data, but signing up a user or saving a form gives a 500, and the log has a line such as sqlite3.OperationalError: attempt to write a readonly database in Python, or SqliteError: attempt to write a readonly database (SQLITE_READONLY) in Node.js. Reading works; only writing is blocked. Its cousin, unable to open database file, means SQLite couldn’t open the file or folder at all.

The most likely causes, in order

1. The file has no write permission

This happens after extracting a ZIP or copying files from another machine, so the database arrives as 444 or 400.

Check: over SSH:

ls -l /home/USER/myapp/db.sqlite3

The line should start with -rw-. Or open File Manager and look at the Permissions column.

Fix:

chmod 644 /home/USER/myapp/db.sqlite3

The chmod calculator works out the values, and file permissions explains them.

2. The folder holding the database isn’t writable

Check: ls -ld /home/USER/myapp should start with drwx.

Fix: chmod 755 /home/USER/myapp. Better still, give the database its own folder (step 4).

3. Another user owns the file

This happens when the file was created by a tool or process outside your account, for example from a restored backup. chmod won’t help then.

Check: the third column of ls -l should be your account name.

Fix: make a fresh copy that you own in place of the old one:

cd /home/USER/data
cp app.db app.db.new && mv app.db.new app.db

If that’s refused, ask support to correct the ownership. See also permission denied on files.

4. The database lives where deploys overwrite it

If db.sqlite3 is inside the repository, every Git deploy or ZIP upload replaces the file and resets its permissions, and can wipe new data. Inside public_html, anyone may be able to download it.

Fix: move it to a folder outside the site and use an absolute path:

mkdir -p /home/USER/data && chmod 755 /home/USER/data
mv /home/USER/myapp/db.sqlite3 /home/USER/data/app.db
# Django: settings.py
DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3",
                         "NAME": "/home/USER/data/app.db"}}

In Prisma use DATABASE_URL="file:/home/USER/data/app.db", and add the file to .gitignore.

5. A relative path opens a different file

Under Passenger the current directory may differ from the project folder, so a relative ./db.sqlite3 opens a file somewhere unexpected, with errors such as no such table or unable to open database file. An absolute path, as above, fixes it.

How to confirm it’s fixed

Try a write from the command line:

sqlite3 /home/USER/data/app.db "CREATE TABLE IF NOT EXISTS _t(x); DROP TABLE _t;"

No error means writes work. Then restart the app (Restart or touch tmp/restart.txt) and save a form.

Prevention

  • Never commit the database file to Git or keep it inside public_html.
  • Read an absolute path from an environment variable.
  • Move to MySQL as users grow; create it in MySQL Databases (guide).

On ArabyHost

On ArabyHost every account runs inside CageFS, and Node.js and Python apps on AI/Developer plans run as your account’s user, so 644 and 755 are enough for files you own. SSH on those plans gives you the ls and chmod commands above, and File Manager can change permissions on every plan. Araby AI reads the error log and explains the fix, and you can create a MySQL/MariaDB database in cPanel when the site grows.

FAQ

Why does the folder need write permission, not just the file?

While writing, SQLite creates a temporary file next to the database, such as `db.sqlite3-journal` or `db.sqlite3-wal`. If it can't create it in the folder, the write fails even when the file itself is writable.

Should I just chmod 777 the file?

No. The app runs as your account, so `644` for the file and `755` for the folder are enough. 777 doesn't fix wrong ownership and lets others write to your data.

Is SQLite fine for a production site?

For small sites with few writes, yes. When many users write at once you'll see `database is locked`; then move to the MySQL/MariaDB the hosting provides.

Useful? Share it with someone who needs it:WhatsAppXLinkedIn

Read next

Ready to put your idea online?

Fast, isolated hosting, support that understands you, and an AI assistant that fixes problems with you.