Report this

What is the reason for this report?

Django db.sqlite3 database is not connected with apache server.

Posted on July 5, 2020

I got main problem in my project,actually Django db.sqlite3 database is not connected with apache server. that’s why when i open my site.I got this error OperationalError at / attempt to write a readonly database

So please tell me its solution. How to connect db.sqlite with apache server in django app on ubuntu.



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!

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.

Hi there @akshaykoshti401,

Can you share the exact database configuration that you are using in your settings.py file?

Usually, in order to configure SQLite with Django, you need to do the following:

  • First install SQLite:
sudo apt install sqlite3
  • Update your settings.py and adjust these 2 lines:
DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = '/path/to/your/project/yourdb'
  • Then run your migrations
python manage.py migrate

Another thing that I could suggest is, rather than using SQLite is to use PostgreSQL, you can take a look at this step by step tutorial on how to configure that:

https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04

Hope that this helps! Regards, Bobby

Heya,

The error OperationalError at / attempt to write a readonly database occurs when the SQLite database file doesn’t have the correct permissions for Apache to write to it.


1. Check File Permissions

Ensure that the db.sqlite3 file and its directory are writable by the user under which Apache is running (typically www-data on Ubuntu).

Run the following commands to set the correct permissions:

# Change ownership of the SQLite database file to www-data
sudo chown www-data:www-data /path/to/your/db.sqlite3

# Change ownership of the directory containing the database file
sudo chown www-data:www-data /path/to/your/

# Set appropriate permissions on the database file
sudo chmod 664 /path/to/your/db.sqlite3

2. Verify Apache User

Ensure Apache is running under the correct user (www-data on Ubuntu by default). You can check this by running:

ps aux | grep apache2

If Apache is not running under www-data, adjust the ownership commands above for the correct user.


3. Check Directory Permissions

SQLite needs write access to the directory containing the database file for temporary files. Ensure the directory containing db.sqlite3 has the right permissions:

sudo chmod 775 /path/to/your/

4. Disable SELinux (if enabled)

If SELinux is enabled, it might block Apache from accessing the database. Check its status:

sestatus

If it’s enabled, you can temporarily disable it to test if this resolves the issue:

sudo setenforce 0

To make the change permanent, edit /etc/selinux/config and set SELINUX=disabled.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.