Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

python - How to set up a PostgreSQL database in Django

I'm new to Python and Django.

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

C:xampphtdocsdjangodir>python manage.py syncdb
Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_manager(settings)
  File "C:Python27libsite-packagesdjangocoremanagement\__init__.py", line
438, in execute_manager
    utility.execute()
  File "C:Python27libsite-packagesdjangocoremanagement\__init__.py", line
379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:Python27libsite-packagesdjangocoremanagement\__init__.py", line
261, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "C:Python27libsite-packagesdjangocoremanagement\__init__.py", line
67, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File "C:Python27libsite-packagesdjangoutilsimportlib.py", line 35, in im
port_module
    __import__(name)
  File "C:Python27libsite-packagesdjangocoremanagementcommandssyncdb.py"
, line 7, in <module>
    from django.core.management.sql import custom_sql_for_model, emit_post_sync_
signal
  File "C:Python27libsite-packagesdjangocoremanagementsql.py", line 6, in
 <module>
    from django.db import models
  File "C:Python27libsite-packagesdjangodb\__init__.py", line 77, in <modul
e>
    connection = connections[DEFAULT_DB_ALIAS]
  File "C:Python27libsite-packagesdjangodbutils.py", line 92, in __getitem
__
    backend = load_backend(db['ENGINE'])
  File "C:Python27libsite-packagesdjangodbutils.py", line 33, in load_back
end
    return import_module('.base', backend_name)
  File "C:Python27libsite-packagesdjangoutilsimportlib.py", line 35, in im
port_module
    __import__(name)
  File "C:Python27libsite-packagesdjangodbackendspostgresqlase.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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

(I prefer to use pip over easy_install for no reason.)

  • $ 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',
    }
}

- Other installation instructions can be found at download page and install page.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...