whinytween96
whinytween96

Reputation: 1965

"manage.py runserver" gives error: django.db.utils.OperationalError: unable to open database file

I'm getting this error running manage.py runserver:

$ python manage.py runserver
...
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

settings.py:

    DATABASES = {
        'default': dj_database_url.config(
            default="sqlite:///{}".format(
                os.path.join(BASE_DIR, 'db/db.sqlite3')
            )
        )
    }

Upvotes: 17

Views: 40388

Answers (13)

Happy Patel
Happy Patel

Reputation: 1688

I have a similar problem. Running as root fixes it:

sudo python3 manage.py runserver

Upvotes: 1

CodeRed
CodeRed

Reputation: 1410

You do not need sudo for this. The solution is to create a 'db' folder in your project root directory before running the migrate command. Apparently, 'manage.py' does not have permission to create folders on your machine, so create the folder and everything should work.

Upvotes: 0

Alireza
Alireza

Reputation: 6848

The issue in my case was to use double forward slash instead of triple forward slash:

I had to change:

os.environ.get("DATABASE_URL", f"sqlite://{BASE_DIR}/db.sqlite3")

To:

os.environ.get("DATABASE_URL", f"sqlite:///{BASE_DIR}/db.sqlite3")

Upvotes: 0

Meet Bhut
Meet Bhut

Reputation: 1

check folder name, you store database file

Upvotes: -1

Barnabas
Barnabas

Reputation: 116

For many users(like myself), who might have this error while using another DB engine but have set the default engine via your env variable invocation as sqllite3, you might want to either remove that default value or check that the env variable you're checking is named the same way in your .env file.

Rookie mistake but well,

Upvotes: 0

Frank
Frank

Reputation: 154

If you are experiencing this problem on AWS EC2 node and using apache, to solve the problem I had to:

chown -R apache:apache project_folder

Upvotes: 1

infected-mushroom
infected-mushroom

Reputation: 21

TO ANY WINDOWS USERS:

I got the same error while creating my first django project. I couldn't use sudo on windows, so the command python manage.py runserver only worked when I ran my terminal as administrator.

Upvotes: 2

Sam
Sam

Reputation: 2200

For me, the issue was that I had two settings files; one for production and one for development. In my manage.py, I'd specified the deployment settings file and forgotten to add manage.py to my .gitignore, so when I ran the project locally it failed when trying to find the production database.

Upvotes: 1

thierno
thierno

Reputation: 934

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')
        )
    )
}

Upvotes: 4

Sudhir Menon
Sudhir Menon

Reputation: 41

@whinytween96 : I have seen this problem occur more when sudo is used to run some commands and not for others. You need to be consistent with the usage of sudo to fix this problem. I had the DB issue and I fixed it by running the command again with sudo prefixed.

i.e.

  1. sudo python manage.py runserver
  2. sudo python manage.py makemigrations
  3. sudo python manage.py migrate

fixed the problem for me.

Upvotes: -1

GitOnion
GitOnion

Reputation: 41

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

Upvotes: 4

Marcelo
Marcelo

Reputation: 119

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.

Upvotes: 11

vanadium23
vanadium23

Reputation: 3586

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

Upvotes: 8

Related Questions