Milad Jafari
Milad Jafari

Reputation: 1155

"unable to open database file" in django

I have a problem with sqlite3 in django. This is the first time I'm using this.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': '/home/djangobook/mydb.db',
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    }
}

error:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/__init__.py", line 324, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py", line 306, in _cursor
    self._sqlite_create_connection()
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py", line 296, in _sqlite_create_connection
    self.connection = Database.connect(**kwargs)
OperationalError: unable to open database file

thanks for your help.

Upvotes: 0

Views: 2619

Answers (3)

acristu
acristu

Reputation: 771

Quick answer from me because there is one other possibility which hit me while playing with a Graphite deployment (graphite's web UI is httpd+django). Even if you have the full path configured, permissions are ok on both the db file and the directory, you might have Selinux Enforcing.

If you need selinux enforcing, sorry, I cannot provide details right now on how to make this setup work, but if it is accidentally enabled just do :

root # getenforce
Enforcing
root # setenforce 0
root # getenforce
Permissive

One tip, to debug this issue deeper and confirm it is an issue related to permissions you could use strace :

$ sudo service httpd stop
$ sudo strace -f service httpd start 2>&1 | tee tmp.log

# strace log could contain something similar to the following:
$ grep mydb.db tmp.log 
.... stat("/home/djangobook/mydb.db", 0x7fff55023b80) = -1 EACCES (Permission denied)
.... open("/home/djangobook/mydb.db", O_RDWR|O_CREAT, 0644) = -1 EACCES (Permission denied)

Upvotes: 1

Victor Castillo Torres
Victor Castillo Torres

Reputation: 10811

You haven't run syncdb

Into your application folder at the same level than manage.py run this in console:

python manage.py syncdb 

For better understanding of django, follow this tutorial by the way you have to write access to the folder.

Upvotes: 3

Ferran
Ferran

Reputation: 14993

You need write access to the /home/djangobook/ directory and to the /home/djangobook/mydb.db file (is exists).

If the /home/djangobook/mydb.db file does not exist you need to run python manage.py syncdb as pointed by @user1788781.

Upvotes: 3

Related Questions