61

I get this error when setting up a server in Django. It is sqlite3 which means it should create the .db file but it doesn't seem to be doing so. I've stipulated SQLite as the backend and an absolute file path for where to put it, but no luck.

Is this a bug or am I doing something incorrect? (Was just thinking, is the absolute file path specified differently in Ubuntu?)

Here is the beginning of my settings.py file:

# Django settings for OmniCloud project.

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
# ('Your Name', 'your_email@example.com'),
)

MANAGERS = ADMINS

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
    'NAME': '~/Harold-Server/OmniCloud.db',                      # Or path to database file if using sqlite3.
    'USER': '',                      # Not used with sqlite3.
    'PASSWORD': '',                  # Not used with sqlite3.
    'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
    'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
}
}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Chris
  • 11,819
  • 19
  • 91
  • 145

8 Answers8

87

Django NewbieMistakes

PROBLEM You're using SQLite3, your DATABASE_NAME is set to the database file's full path, the database file is writeable by Apache, but you still get the above error.

SOLUTION Make sure Apache can also write to the parent directory of the database. SQLite needs to be able to write to this directory.

Make sure each folder of your database file's full path does not start with number, eg. /www/4myweb/db (observed on Windows 2000).

If DATABASE_NAME is set to something like '/Users/yourname/Sites/mydjangoproject/db/db', make sure you've created the 'db' directory first.

Make sure your /tmp directory is world-writable (an unlikely cause as other thing on your system will also not work). ls /tmp -ald should produce drwxrwxrwt ....

Make sure the path to the database specified in settings.py is a full path.

Also make sure the file is present where you expect it to be.

akki
  • 2,021
  • 1
  • 24
  • 35
John
  • 5,166
  • 27
  • 31
  • How would I change the permissions of Apache via command line in Linux? – Chris Oct 06 '11 at 14:32
  • You don't change the permissions of apache you change the permissions of the file and folder so that Apache can read and write in the correct places. Here is a guide to chmod http://catcode.com/teachmod/ which is how you change permissions in linux. though it should just be chmod +rw folder_name – John Oct 06 '11 at 16:15
  • `chmod o+w .` in the DB file's folder fixed this for me. – lionello Oct 30 '15 at 17:17
  • From the same website, if you're on Windows for anybody else who might need it: "If you working on windows make also sure that you have the path to the db directory written with double backlashes (using os.path methods are recommended) `'C:\\django\\sqlite\\django.db'` or `r'C:\django\sqlite\django.db'` Make sure there is no special charcaters in the path like "éè" or "(". If you are using Windows and this message appears intermittently make sure that your security software (Anti-malware) are not opening (and locking) your database file to check for malware presence." – blue_chip Apr 30 '19 at 18:46
21

I faced exactly same issue. Here is my setting which worked.

'ENGINE': 'django.db.backends.sqlite3', 
'NAME': '/home/path/to/your/db/data.sqlite3'

Other setting in case of sqlite3 will be same/default.
And you need to create data.sqlite3.

chhantyal
  • 11,874
  • 7
  • 51
  • 77
11

You haven't specified the absolute path - you've used a shortcut , ~, which might not work in this context. Use /home/yourusername/Harold-Server/OmniCloud.db instead.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • would username be the name of the server or, since I'm logged in as root, root? – Chris Oct 07 '11 at 04:09
  • Don't log in as root, ever. Set up a user for the Django site, or use the Apache user (usually www-data). – Daniel Roseman Oct 07 '11 at 08:01
  • Okay I am accessing the linode server via ssh and added a user named Ned. So I need to pull the repo from github and then run it on Ned? – Chris Oct 07 '11 at 13:00
7

You need to use full path instead of ~/.

In your case, something like /home/harold/Harold-Server/OmniCloud.db.

Chris McFarland
  • 6,059
  • 5
  • 43
  • 63
6

In my case the sqlite db file db.sqlite3 was stored in the DocumentRoot of apache. So, even after setting the following permissions it didn't work:

sudo chown www-data:www-data /path/to/db-folder
sudo chown www-data:www-data /path/to/db-folder/sqlite-db.db

Finally when i moved db.sqlite3 to a newly created folder dbfolder under DocumentRoot and gave the above permissions, and it worked.

bprasanna
  • 2,423
  • 3
  • 27
  • 39
2

I had this problem serving with Apache and found that using the absolute path to the sqlite3 db in my .env //// as opposed to using the relative path /// fixed the problem. All of the permissions and ownership mentioned above are necessary as well.

rick debbout
  • 439
  • 2
  • 5
  • 19
1

use this type it works for me . windows 7 with python 2.7 and django 1.5

'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'C:\\tool\\mysite\\data.db',

hope its works...

0

In the same case, the problem was in empty write permissions.

sudo chmod -R o+w ./dir/
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 12 '23 at 06:09