Question

Django can't get access to sqlite3 database

I am currently running a server instance of the one-click install option for Django Ubuntu 14.04.

This server is running a Django app using NGINX, sqlite3, and gunicorn.

I set up letsencrypt properly and it is used to serve my website with ssl.

I deleted all of the files in /home/django/dango_project/ and replaced them with my own.

When I load my website, everything works fine, except for my admin site.

I am able to log in to the admin site when I run the django_project locally, but it isn’t working when the project is in production at my website.

You can verify that my db.sqlite3 file exists and contains posts by going to kensch.com/blog.

With debug disabled, my website gives a Server Error (500) after I log in to my admin site.

With debug enabled, my website gives OperationalError at /admin/ attempt to write a readonly database after logging into the admin site.

I have been trying to get into my admin site for nearly 5 hours now, no google searches are helping.

I appreciate any help in advance!

Show comments

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
January 31, 2024
Pinned Answer

The “attempt to write a readonly database” error typically occurs when the user running the Gunicorn process doesn’t have write permissions on the SQLite database file. Since you mentioned that you are using the one-click install option for Django on Ubuntu 14.04, it’s possible that this issue is related to file permissions. Here’s how to address it:

  1. Check the User Running Gunicorn:

    First, you should check which user is running the Gunicorn process. You can do this by examining the systemd service unit file for Gunicorn:

sudo systemctl cat gunicorn
Look for the `User` and `Group` directives in the output. By default, Gunicorn may be running as a user like `www-data`.
  1. Check Permissions:

    Ensure that the SQLite database file and the directory containing it have the correct permissions. You should grant write permissions to the user running Gunicorn. Replace your_database.db with the actual name of your database file:

sudo chown www-data:www-data /path/to/your/database/your_database.db
Additionally, make sure that the parent directory of your database file also has the correct permissions:
sudo chown www-data:www-data /path/to/your/database/
This ensures that both the database file and its containing directory are writable by the user running Gunicorn.
  1. Restart Gunicorn and Nginx:

    After changing permissions, restart Gunicorn and Nginx to apply the changes:

sudo systemctl restart gunicorn 
sudo systemctl restart nginx

open puTTy once you link the ip address and login cd into file location ie cd /home/django/django_project/ then enter

chown django:django db.sqlite3

Through research I found out that someone answered–and it worked for me–the solution perfectly (sorry, I did so much research that I do not remember who or where was it):

cd project-folder sudo chown www-data .

Mind the space between www-data and the period.

Best,

Laurentino

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel