Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django.db.utils.OperationalError: unable to open database file

Tags:

django

When I run

python manage.py runserver

I get this error

File "/usr/local/lib/python2.7/dist-packages/Django-1.10.1-py2.7.egg/django/db/backends/sqlite3/base.py", line 209, in get_new_connection conn = Database.connect(**conn_params) django.db.utils.OperationalError: unable to open database file

my settings.py:

DATABASES = {
    'default': dj_database_url.config(
        default="sqlite:///{}".format(
            os.path.join(BASE_DIR, 'db/db.sqlite3')
        )
    )
}
like image 631
whinytween96 Avatar asked Dec 04 '16 13:12

whinytween96


5 Answers

Just remove the first db in your settings.py file.

You have os.path.join(BASE_DIR, 'db/db.sqlite3') if you remove the first db, you'll have os.path.join(BASE_DIR, 'db.sqlite3')

Your database settings will be

DATABASES = {
    'default': dj_database_url.config(
        default="sqlite:///{}".format(
            os.path.join(BASE_DIR, 'db.sqlite3')
        )
    )
}
like image 83
thierno Avatar answered Oct 07 '22 09:10

thierno


I have similar problem.

You just run command with sudo

sudo python3 manage.py runserver

Enjoy!

like image 36
Happy Patel Avatar answered Oct 07 '22 07:10

Happy Patel


Suffering for a while from the same issue and I believe I have found a solution, finally!

sudo python manage.py runserver

and that did the trick for me.

My permissions were telling me that everything is how it should be. Regardless, I still had to type in sudo at the beginning of the command at the terminal.

like image 11
Marcelo Avatar answered Nov 05 '22 01:11

Marcelo


Basically there are two answers, either user which running server don't have rights to open database file. You can try to fix this by:

sudo chown $(whoami):$(whoami) /path/to/dir/db/db.sqlite3

Or you don't have this file, you can create it by applying migrate command:

./manage.py migrate
like image 6
vanadium23 Avatar answered Nov 05 '22 01:11

vanadium23


I had the same problem, just solved it. Make sure www-data (or whatever daemon running your web server) has access to both the db.sqlite3 file, and also the path to it. So:

sudo chown :www-data <project_folder>/
sudo chown :www-data <project_folder>/db.sqlite3
sudo chmod 664 <project_folder>/db.sqlite3
like image 4
GitOnion Avatar answered Nov 05 '22 00:11

GitOnion