在windows中为django设置postgresql

问题描述 投票:4回答:2

我正在学习django,在这里我正在尝试将数据库从默认的sqlite3更改为postgresql。我在互联网上搜索过(google,bing,duckduckgo,..),但没有结果。

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'websitedb',
        'USER': 'postgres',
        'PASSWORD': 'teamarrow',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}`  

在settings.py,Installed postgresql中更改了数据库,从here为windows安装了psycopg2,在pgADMINIII中创建了一个新数据库。

在命令提示符python manage.py runserver。这是错误:

 ` Traceback (most recent call last): 
 File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\Admin\Pictures\website\lib\site-packages\django\core\management\__ini
t__.py", line 385, in execute_from_command_line
    utility.execute()
  File "C:\Users\Admin\Pictures\website\lib\site-packages\django\core\management\__ini
t__.py", line 354, in execute
    django.setup()
  File "C:\Users\Admin\Pictures\website\lib\site-packages\django\__init__.py", line 21
, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\Admin\Pictures\website\lib\site-packages\django\apps\registry.py", li
ne 108, in populate
    app_config.import_models(all_models)
  File "C:\Users\Admin\Pictures\website\lib\site-packages\django\apps\config.py", line
 202, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Python27\Lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
  File "C:\Users\Admin\Pictures\website\lib\site-packages\django\contrib\auth\models.p
y", line 40, in <module>
    class Permission(models.Model):
  File "C:\Users\Admin\Pictures\website\lib\site-packages\django\db\models\base.py", l
ine 122, in __new__
    new_class.add_to_class('_meta', Options(meta, **kwargs))
  File "C:\Users\Admin\Pictures\website\lib\site-packages\django\db\models\base.py", l
ine 297, in add_to_class
    value.contribute_to_class(cls, name)
  File "C:\Users\Admin\Pictures\website\lib\site-packages\django\db\models\options.py"
, line 166, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "C:\Users\Admin\Pictures\website\lib\site-packages\django\db\__init__.py", line
 40, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "C:\Users\Admin\Pictures\website\lib\site-packages\django\db\utils.py", line 24
2, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "C:\Users\Admin\Pictures\website\lib\site-packages\django\db\utils.py", line 10
8, in load_backend
    return import_module('%s.base' % backend_name)
  File "C:\Python27\Lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
  File "C:\Users\Admin\Pictures\website\lib\site-packages\django\db\backends\postgresq
l_psycopg2\base.py", line 27, in <module>
    raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module n
amed psycopg2

请调试错误的位置。

python django postgresql virtualenv psycopg2
2个回答
0
投票

您没有安装PostgreSQL适配器(psycopg2):

Error loading psycopg2 module: No module named psycopg2

您应该下载正确的版本并首先安装它:

http://www.lfd.uci.edu/~gohlke/pythonlibs/#psycopg

更新:由于问题的格式不正确,我无法看到您已经下载了它。您是否考虑过“虚拟环境安装说明”中的注释?使用virtualenv时,不应双击安装程序exe。相反,你应该使用setuptoolsdistribute将软件包安装到你的virtualenv中。有关详细信息,请参阅http://www.stickpeople.com/projects/python/win-psycopg/


0
投票

只是安装

pip install pyscopg2

在你的文件夹下

© www.soinside.com 2019 - 2024. All rights reserved.