Django is "unable to open database file"
Asked Answered
V

9

41

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 :(

Var answered 19/7, 2010 at 11:5 Comment(1)
Make sure that www-data can walk the entire path to your db, and not just read/write the db file.Inconsistency
V
17

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.

Var answered 19/7, 2010 at 12:50 Comment(1)
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.Interposition
B
50

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.

Bioastronautics answered 5/4, 2012 at 7:22 Comment(3)
The parent directory permission was non-obvious (to me); thanks.Triad
There's an FAQ entry here as well.Ir
So should I really fire chmod 777 on database-containing directory?Accompany
V
17

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.

Var answered 19/7, 2010 at 12:50 Comment(1)
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.Interposition
A
6

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.:))

Authorized answered 7/4, 2012 at 16:59 Comment(0)
F
4

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
Ferricyanide answered 14/6, 2016 at 5:12 Comment(0)
M
3

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.

Melesa answered 28/6, 2012 at 16:58 Comment(0)
B
1

Just rename the database file with .sqlite3 extension, that works with me.

Barbados answered 9/12, 2020 at 13:9 Comment(0)
S
0

To change the permission of parent directory, you can use below set of commands:

check apache process for apache v2:

ps -ef | grep apache | grep -v grep

the user/group is www-data

chgrp www-data /path/to/mydir
chmod g+w /path/to/mydir

Ref: https://askubuntu.com/questions/58725/how-do-we-know-that-a-directory-is-apache-writable

Sammy answered 13/4, 2018 at 14:21 Comment(0)
G
0

Check if the folder is in the Windows Ransomware Protection. Remove it if it is.

Griseofulvin answered 16/5, 2022 at 16:15 Comment(0)
H
0

I came across this while using Docker. I needed to set "volumes" in my docker server service in compose.yaml. My file looks like this:

services:
  server:
    build:
      context: .
    volumes:
      - .:/app
    ports:
      - 8000:8000

Edit: This matches dockerfile (created by default) which contains:

WORKDIR /app

Homoeroticism answered 19/6 at 8:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.