开发者

Django is "unable to open database file"

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开发者_StackOverflow 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 :(


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.


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.


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


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.


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


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜