如何在Django中设置PostgreSQL数据库

如何在Django中设置PostgreSQL数据库

本文介绍了如何在Django中设置PostgreSQL数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很习惯Python和Django。



我正在使用PostgreSQL数据库引擎后端配置Django项目,但是我在每个数据库操作上都收到错误,例如当我运行 manage.py syncdb ,我得到:

  C:\xampp\htdocs\djangodir> python manage.py syncdb 
追溯(最近调用最后):
文件manage.py,第11行在< module> ;
execute_manager(settings)
文件C:\Python27\lib\site- packages\django\core\management\__init __。py,行
438,在execute_manager
utility.execute()
文件C:\Python27\lib\site- packages\django\core\management\__init __。py,行
379,执行
self.fetch_command(子命令).run_from_argv(self.argv)
文件C:\Python27\lib\site- packages\django\core\management \__init __。py,行
261,fetch_command
klass = load_command_class(app_name,子命令)
文件C:\Python27\lib\site-packages\django \core\management\__init __。py,行
67,在load_command_class
module = import_module('%s.management.commands。%s'%(app_name,name))
文件C:\Python27\lib\site- packages\django\utils\importlib.py,第35行,im
port_module
_ _import __(name)
文件C:\Python27\lib\site- packages\django\core\management\commands\syncdb.py
,第7行, <模块>
从django.core.management.sql导入custom_sql_for_model,emit_post_sync_
信号
文件C:\Python27\lib\site- packages\django\core\management\\ \\ sql.py,第6行,
< module>
从django.db导入模型
文件C:\Python27\lib\site- packages\django\db\__init __。py,第77行,< modul
e>
connection = connections [DEFAULT_DB_ALIAS]
文件C:\Python27\lib\site-packages\django\db\utils.py,第92行,__getitem
__
backend = load_backend(db ['ENGINE'])
文件C:\Python27\lib\site-packages\django\db\utils.py,行33,in load_back
end
return import_module('。base',backend_name)
文件C:\Python27\lib\site- packages\django\utils\ importlib.py,第35行,im
port_module
__import __(name)
文件C:\Python27\lib\site-packages\django\db\ backends\postgresql\base.py,li
ne 23,在< module>
raise不正确配置(加载psycopg模块:%s错误%e)
django.core.exceptions.ImproperlyConfigured:加载psycopg模块时出错:否mo
dule命名为psycopg

有人可以告诉我发生了什么吗?

解决方案

你需要安装 Python库。



安装






下载,然后将其安装在Python PATH



下载后,轻松解压缩tarball:

  $ python setup.py install 

或者如果你愿意,请通过或。



>




  • $ easy_install psycopg2

  • $ pip安装psycopg2



配置






设置 .py

  DATABASES = {
'default':{
'ENGINE':'django.db.backends.postgresql',
'NAME':'db_name',
'USER ':'db_user',
'PASSWORD':'db_user_password',
'HOST':'',
'PORT':'db_port_number',
}







I'm new to Python and Django.

I'm configuring a Django project using PostgreSQL database engine backend, But I'm getting errors on each database operations, for example when i run manage.py syncdb, I'm getting:

C:\xampp\htdocs\djangodir>python manage.py syncdb
Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_manager(settings)
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
438, in execute_manager
    utility.execute()
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
261, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
67, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File "C:\Python27\lib\site-packages\django\utils\importlib.py", line 35, in im
port_module
    __import__(name)
  File "C:\Python27\lib\site-packages\django\core\management\commands\syncdb.py"
, line 7, in <module>
    from django.core.management.sql import custom_sql_for_model, emit_post_sync_
signal
  File "C:\Python27\lib\site-packages\django\core\management\sql.py", line 6, in
 <module>
    from django.db import models
  File "C:\Python27\lib\site-packages\django\db\__init__.py", line 77, in <modul
e>
    connection = connections[DEFAULT_DB_ALIAS]
  File "C:\Python27\lib\site-packages\django\db\utils.py", line 92, in __getitem
__
    backend = load_backend(db['ENGINE'])
  File "C:\Python27\lib\site-packages\django\db\utils.py", line 33, in load_back
end
    return import_module('.base', backend_name)
  File "C:\Python27\lib\site-packages\django\utils\importlib.py", line 35, in im
port_module
    __import__(name)
  File "C:\Python27\lib\site-packages\django\db\backends\postgresql\base.py", li
ne 23, in <module>
    raise ImproperlyConfigured("Error loading psycopg module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg module: No mo
dule named psycopg

Can someone give me a clue on what is going on?

解决方案

You need to install psycopg2 Python library.

Installation


Download http://initd.org/psycopg/, then install it under Python PATH

After downloading, easily extract the tarball and:

$ python setup.py install

Or if you wish, install it by either easy_install or pip.

  • $ easy_install psycopg2
  • $ pip install psycopg2

Configuration


in settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'db_name',
        'USER': 'db_user',
        'PASSWORD': 'db_user_password',
        'HOST': '',
        'PORT': 'db_port_number',
    }
}


这篇关于如何在Django中设置PostgreSQL数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 03:40