How to set up a PostgreSQL database in Django

I am new to Python and Django.

I am setting up a Django project using the PostgreSQL database engine, but I get errors with every database operation. For example, when I run manage.py syncdb , I get:

 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 tell me what is going on?

+99
python django postgresql django-settings psycopg2
Mar 22 '11 at 16:13
source share
11 answers

You need to install psycopg2 Python library.

Installation




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

After downloading, easily extract the archive and:

 $ python setup.py install 

Or if you want, install it easy_install or pip .

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

  • $ easy_install psycopg2
  • $ pip install psycopg2

Configuration




in .py settings

 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 on the download page and install the page .

+199
Mar 24 2018-11-11T00:
source share

Also make sure that the PostgreSQL development kit is installed. On Ubuntu, you need to do something like this:

 $ sudo apt-get install libpq-dev 
+28
Aug 16 2018-12-12T00:
source share

Step by step what 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 can install a graphical database management tool, for this you can do:

 sudo apt-get install postgresql pgadmin3 

After that, you should change the password of the Postgre user, then do:

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

In your settings.py file you do:

 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'dbname', 'USER': 'postgres', 'PASSWORD': 'postgres', 'HOST': '', 'PORT': '', } } 
+17
Sep 28 '14 at 16:52
source share

This may seem a bit long, but it worked for me without error.

First install phppgadmin from the Ubuntu Software Center.

Then follow these steps in the terminal.

 sudo apt-get install libpq-dev python-dev pip install psycopg2 sudo apt-get install postgresql postgresql-contrib phppgadmin 

Launch Apache Server

 sudo service apache2 start 

Now run this too in the terminal to edit the apache file.

 sudo gedit /etc/apache2/apache2.conf 

Add the following line to the open file:

 Include /etc/apache2/conf.d/phppgadmin 

Now reload apache. Use a terminal.

 sudo /etc/init.d/apache2 reload 

Now you will need to create a new database. Log in as a postgres user. Continue to terminal.

 sudo su - postgres 

If you are having problems with the "postgres" password, you can change it using the answer here https://stackoverflow.com/a/16629/16 and continue the steps.

Now create a database

 createdb <db_name> 

Now create a new user to log in to phppgadmin later by providing a new password.

 createuser -P <new_user> 

Now your postgressql is configured and you can go to:

 http://localhost/phppgadmin/ 

and log in using the new user you created to view the database.

+9
May 08 '15 at 18:57
source share

You can install "psycopg" with the following command:

 # sudo easy_install psycopg2 

Alternatively, you can use pip:

 # pip install psycopg2 

easy_install and pip are included in ActivePython or manually installed from projects .

Or just download the preinstalled Windows installer .

+6
Mar 22 '11 at 17:20
source share

The immediate problem is that you are missing psycopg .

+4
Mar 22 '11 at 16:16
source share

If you are using Fedora 20, Django 1.6.5, postgresql 9.3. * and you need the psycopg2 module, do the following:

 yum install postgresql-devel easy_install psycopg2 

If you are like me, you may have trouble finding a well-documented libpq-dev rpm file ... The above worked for me just now.

+3
Jun 23 '14 at 2:30
source share

I had the same issue on a Mac .

The solution was to use only PIP to set everything up and touch on some things.

First install PIP from: https://pip.pypa.io/en/latest/

Then you want to make sure the path to pg_config is in PATH (echo $ PATH) if you cannot edit your bash_profile file:

 vi /Users/<user>/.bash_profile 

and add this line:

 export PATH=$PATH:/path/to/pg_config/bin 

If you do not know where pg_config is, you can use the "locate" tool, but make sure your locate.db is updated (I used the old locate.db file and used paths that do not exist).

 sudo /usr/libexec/locate.updatedb locate pg_config 

Then install Django (if necessary) and psycopg2.

 sudo pip install Django sudo pip install psycopg2 

And then in settings.py (localhost: defaultport)

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

Congratulations!

+3
Sep 02 '14 at 17:39
source share
 $ sudo apt-get install libpq-dev 

Year, it solves my problem. After you complete the following steps: pip install psycopg2

+2
Apr 20 '14 at 14:24
source share

Please note that installing psycopg2 through pip or setup.py requires Visual Studio 2008 (more precisely, the vcvarsall.bat executable). If you do not have administrator rights to install it or set the corresponding PATH variable in Windows, you can download the already compiled library from here .

0
Mar 11 '14 at 15:24
source share

This is one of the very good and step-by-step steps for setting up PostgreSQL on an Ubuntu server. I tried this with Ubuntu 16.04 and it works.

https://www.digitalocean.com/community/tutorials/how-to-use-postgresql-with-your-django-application-on-ubuntu-14-04

0
Mar 21 '18 at 15:56
source share



All Articles