Django is "unable to open database file"

48,505

Solution 1

I solved the error by changing the DATABASE_NAME to an absolute path: /var/www/apps/apps.db.

On a windows machine, backslash should be escaped like: C:\\path\\to\\database\\database_name.db.

Solution 2

Solution from NewbieMistakes

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.

Solution 3

DATABASE_NAME is deprecated. You must use the currently supported format. i.e.

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': 'C:/ispdb.sqlite',
    'USER': '',
    'PASSWORD': '',
    'HOST': '',
    'PORT': ''

}

And also see other settings which are deprecated from the django website.:))

Solution 4

Well, I answered it on this question. http://goo.gl/KAuXz

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

'ENGINE': 'django.db.backends.sqlite3', 
'NAME': '/home/neo/django/db/data.sqlite3'

Other setting in case of sqlite3 will be same/default.

Solution 5

For my linux system, I had to give process owner write permission to both the db.sqlite3 and the directory that contained it! You could just setfacl instead! example: (https://serverfault.com/questions/484818/best-way-to-set-up-permissions-with-nginx-php-fpm-on-shared-hosting).

(py2.7)[me@server django-project-container]$ ls -la djangoproject/

drwxrwxr-x. 6 root nginx  4096 Jun 14 01:05 .
drwxr-xr-x. 6 root root  4096 Jun 13 23:47 ..
-rwxrwxrwx. 1 root nginx 49152 Jun 14 01:05 db.sqlite3
Share:
48,505
niklasfi
Author by

niklasfi

Updated on December 10, 2020

Comments

  • niklasfi
    niklasfi over 3 years

    after running "python manage.py syncdb" i gett an error saying "unable to open database file".

    here is the important part from my settings.py:

    DATABASE_ENGINE = 'sqlite3'    # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
    DATABASE_NAME = 'apps.db'      # Or path to database file if using sqlite3.
    DATABASE_USER = ''             # Not used with sqlite3.
    DATABASE_PASSWORD = ''         # Not used with sqlite3.
    DATABASE_HOST = ''             # Set to empty string for localhost. Not used with sqlite3.
    DATABASE_PORT = ''             # Set to empty string for default. Not used with sqlite3.
    

    and here are the permissions for "apps.db":

    -rw-r--r-- 1 root root 33792 19. Jul 10:51 apps.db
    

    My django server is called from apache... i don't know if it has to do with the permissions but changing the owner of apps.db to "www-data" did not work either

    [edit]

    to ensure www-data can access all of this i did the following:

    did the following:

    chown -R www-data apps
    rm apps.db
    su www-data
    python manage.py syncdb
    

    but it still does not work :(

  • Rob Fisher
    Rob Fisher about 11 years
    The parent directory permission was non-obvious (to me); thanks.
  • m01
    m01 over 10 years
    There's an FAQ entry here as well.
  • gajo357
    gajo357 about 10 years
    What if I'm trying to access the database via network and the path starts with "\\MyMachine\..." which translates to "\\\\MyMachine\\..." . Then I can't open the connection, I get "unable to open database file". Even if the database is on my machine, I'm just trying to access it in a different way.
  • Qback
    Qback about 6 years
    So should I really fire chmod 777 on database-containing directory?