Why do I get sqlite error, "unable to open database file"?

Solution 1:

Aha, just stumbled across an article explaining this. Also Django have info on their NewbieMistakes page.

The solution is to make sure the directory containing the database file also has write access allowed to the process.

In my case, running this command fixed the problem:

sudo chown www-data .

Solution 2:

From the Django says "Unable to Open Database File" when using SQLite3 section of the Newbie mistakes Django wiki page:

  1. make sure Apache can also write to the parent directory of the database
  2. make sure none of the folders of the database file's full path start with a number
  3. make sure the full path to the db directory exists
  4. make sure your /tmp directory is world-writable
  5. make sure the path to the database specified in settings.py is a full path
  6. make sure there are no special characters in the path
  7. on Windows, make sure the db directory path is written with double backslashes

Solution 3:

My solution to this was more like so. I didn't really want to change the ownership of this dir. (mostly because i use the pi user to do things like git)

/var/www/mysite $ ls -la sql*
-rw-rw-r-- 1 pi       pi       25600 Jan  2 22:57 sqlite.db

(or whatever db you are using)

where pi is the user that i created all the files in. (yes this is a raspberry pi)

Instead of changing of permissions to www-data, i found that I only needed to change the permissions like this:

sudo chmod 775 /var/www/mysite
sudo chmod 664 /var/www/mysite/sqlite.db
sudo usermod -a -G pi www-data

This gives group write access to the necessary files and adds the www-data user to the pi group.

Note: if you have logging, you will need to do this for the django logfile as well or apache won't like it much.


Solution 4:

Adding an operational user to the www-data group is working well on my test environment. Additionally I've put the sqlite3.db file into a separate subfolder, to be more secure.

Database file shall be owned by www-data

sudo chown www-data mysite/db_sqlite3/
sudo chown www-data mysite/db_sqlite3/my.db

My operational user hape gets a member of www-data group:

sudo usermod -a -G www-data hape

Allow database file write access to members of group www-data:

sudo chmod u+w+x,g+w+x mysite/db_sqlite3/
sudo chmod u+w+x,g+w+x mysite/db_sqlite3/my.db

As a result, the database can be accessed read+write by apache2-daemon (user www-data), without giving grant to the project root folder, and - on the other hand - the app can be run in dev mode by the operational user hape, e.g.

./manage.py runserver

also.


Solution 5:

The solution is to make sure the directory containing the database file also has write access allowed to the process.

For Windows 7, 8.1, 10, Server 2012, etc. follow the Bonobo installation directions:

Allow IIS User to modify C:\inetpub\wwwroot\Bonobo.Git.Server\App_Data folder.

To do so:

  1. select Properties of App_Data folder,
  2. go to Security tab,
  3. click edit,
  4. select IIS user (in my case IIS_IUSRS) and add Modify and Write permission,
  5. confirm these settings with Apply button.

Tags:

Sqlite

Django