How to set up a PostgreSQL database in Django

0 votes

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 syncdb, I'm getting:

C:\xampp\htdocs\djangodir>python syncdb
Traceback (most recent call last):
  File "", line 11, in <module>
  File "C:\Python27\lib\site-packages\django\core\management\", line
438, in execute_manager
  File "C:\Python27\lib\site-packages\django\core\management\", line
379, in execute
  File "C:\Python27\lib\site-packages\django\core\management\", line
261, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "C:\Python27\lib\site-packages\django\core\management\", line
67, in load_command_class
    module = import_module('' % (app_name, name))
  File "C:\Python27\lib\site-packages\django\utils\", line 35, in im
  File "C:\Python27\lib\site-packages\django\core\management\commands\"
, line 7, in <module>
    from import custom_sql_for_model, emit_post_sync_
  File "C:\Python27\lib\site-packages\django\core\management\", line 6, in
    from django.db import models
  File "C:\Python27\lib\site-packages\django\db\", line 77, in <modul
    connection = connections[DEFAULT_DB_ALIAS]
  File "C:\Python27\lib\site-packages\django\db\", line 92, in __getitem
    backend = load_backend(db['ENGINE'])
  File "C:\Python27\lib\site-packages\django\db\", line 33, in load_back
    return import_module('.base', backend_name)
  File "C:\Python27\lib\site-packages\django\utils\", line 35, in im
  File "C:\Python27\lib\site-packages\django\db\backends\postgresql\", 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?

Jun 30, 2020 in Python by kartik
• 37,530 points

1 answer to this question.

0 votes

Hello @kartik,

Steps that I use:

 - sudo apt-get install python-dev
 - sudo apt-get install postgresql-server-dev-9.1
 - sudo apt-get install python-psycopg2 - Or sudo pip install psycopg2

You may want to install a graphic tool to manage your databases, for that you can do:

sudo apt-get install postgresql pgadmin3 

After, you must change Postgre user password, then do:

 - sudo su
 - su postgres -c psql postgres
 - ALTER USER postgres WITH PASSWORD 'YourPassWordHere';
 - \q

On your file you do:

    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'dbname',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': '',
        'PORT': '',

Hope this helps!!

Thank you!!

answered Jun 30, 2020 by Niroj
• 82,820 points

Related Questions In Python

0 votes
1 answer

How to clear a database from the CLI with in Django?

Hello @kartik, To drop the database and run syncdb again. ...READ MORE

answered Aug 7, 2020 in Python by Niroj
• 82,820 points
0 votes
2 answers

How do I connect to a MySQL Database in Python?

connect mysql database with python import MySQLdb db = ...READ MORE

answered Mar 28, 2019 in Python by rajesh
• 1,230 points
0 votes
1 answer

How to round a floating point number up to certain decimal place in Python?

This is normal (and has nothing to do ...READ MORE

answered Oct 8, 2018 in Python by charlie_brown
• 7,780 points
0 votes
1 answer

How to run django unit-tests on production database in Python?

In case someone googles here searching for ...READ MORE

answered Nov 28, 2018 in Python by Nymeria
• 3,520 points
0 votes
1 answer

How to temporarily disable a foreign key constraint in MySQL?

Hello @kartik, To turn off foreign key constraint ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,820 points
0 votes
1 answer

How do I use Django templates without the rest of Django?

Hello @kartik, Let's say you have this important ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,820 points
0 votes
1 answer

How to output text from database with line breaks in a django template?

Hello @kartik, Use linebreaks or linebreaksbr filter: {{ text|linebreaks }} Or surround the text ...READ MORE

answered Jun 24, 2020 in Python by Niroj
• 82,820 points
0 votes
1 answer

How to look up a dictionary value with a variable in Django template?

Hii @kartik, Write a custom template filter: from django.template.defaulttags ...READ MORE

answered Aug 3, 2020 in Python by Niroj
• 82,820 points