问题:如何在Django中设置PostgreSQL数据库

我是Python和Django的新手。

我正在使用PostgreSQL数据库引擎后端配置Django项目,但是每个数据库操作都出现错误。例如,当我跑步时manage.py syncdb,我得到:

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

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

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:\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?


回答 0

您需要安装psycopg2Python库。

安装


下载http://initd.org/psycopg/,然后将其安装在Python PATH下

下载后,轻松解压缩tarball并:

$ python setup.py install

或者,如果愿意,可以通过easy_installpip进行安装。

我更喜欢无缘无故地使用pip而不是easy_install。

  • $ easy_install psycopg2
  • $ pip install psycopg2

组态


设置 .py中

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

-其他安装说明可在下载页面安装页面找到

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.


回答 1

另外,请确保已安装PostgreSQL开发包。在Ubuntu上,您需要执行以下操作:

$ sudo apt-get install libpq-dev

Also make sure you have the PostgreSQL development package installed. On Ubuntu you need to do something like this:

$ sudo apt-get install libpq-dev

回答 2

我使用的分步指南:

 - 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

您可能需要安装图形工具来管理数据库,为此,您可以执行以下操作:

sudo apt-get install postgresql pgadmin3 

之后,您必须更改Postgre用户密码,然后执行以下操作:

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

在您的settings.py文件中,您可以执行以下操作:

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

额外:

如果要使用命令行创建数据库,则可以执行以下操作:

- sudo su
- su postgres -c psql postgres
- CREATE DATABASE dbname;
- CREATE USER djangouser WITH ENCRYPTED PASSWORD 'myPasswordHere';
- GRANT ALL PRIVILEGES ON DATABASE dbname TO djangouser;

在您的settings.py文件中,您可以执行以下操作:

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

Step by step 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 settings.py file you do:

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

Extra:

If you want to create the db using the command line you can just do:

- sudo su
- su postgres -c psql postgres
- CREATE DATABASE dbname;
- CREATE USER djangouser WITH ENCRYPTED PASSWORD 'myPasswordHere';
- GRANT ALL PRIVILEGES ON DATABASE dbname TO djangouser;

On your settings.py file you do:

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

回答 3

这似乎有点冗长,但是对我来说没有任何错误。

首先,从Ubuntu软件中心安装phppgadmin。

然后在终端中运行这些步骤。

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

启动apache服务器

sudo service apache2 start

现在在终端中也运行此命令以编辑apache文件。

sudo gedit /etc/apache2/apache2.conf

将以下行添加到打开的文件中:

Include /etc/apache2/conf.d/phppgadmin

现在重新加载apache。使用终端。

sudo /etc/init.d/apache2 reload

现在,您将不得不创建一个新的数据库。以“ postgres”用户身份登录。在终端继续。

sudo su - postgres

如果您在使用密码“ postgres”时遇到麻烦,可以在此处使用答案https://stackoverflow.com/a/12721020/1990793进行更改,然后继续执行步骤。

现在创建一个数据库

createdb <db_name>

现在,创建一个新用户以稍后登录到phppgadmin,并提供一个新密码。

createuser -P <new_user>

现在您的postgressql已设置完毕,您可以转到:

http://localhost/phppgadmin/

并使用您创建的新用户登录,以查看数据库。

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

At first, Install phppgadmin from Ubuntu Software Center.

Then run these steps in terminal.

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

Start the apache server

sudo service apache2 start

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

sudo gedit /etc/apache2/apache2.conf

Add the following line to the opened file:

Include /etc/apache2/conf.d/phppgadmin

Now reload apache. Use terminal.

sudo /etc/init.d/apache2 reload

Now you will have to create a new database. Login as ‘postgres’ user. Continue in terminal.

sudo su - postgres

In case you have trouble with the password of ‘postgres’, you can change it using the answer here https://stackoverflow.com/a/12721020/1990793 and continue with the steps.

Now create a database

createdb <db_name>

Now create a new user to login to phppgadmin later, providing a new password.

createuser -P <new_user>

Now your postgressql has been setup, and you can go to:

http://localhost/phppgadmin/

and login using the new user you’ve created, in order to view the database.


回答 4

您可以使用以下命令安装“ psycopg”:

# sudo easy_install psycopg2

另外,您可以使用pip:

# pip install psycopg2

easy_install和pip包含在ActivePython中,或从各个 项目站点手动安装。

或者,只需获取预构建的Windows安装程序

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 with ActivePython, or manually installed from the respective project sites.

Or, simply get the pre-built Windows installer.


回答 5

眼前的问题似乎是您缺少了psycopg模块。

The immediate problem seems to be that you’re missing the psycopg module.


回答 6

如果您使用的是Fedora 20,Django 1.6.5,postgresql 9.3。*,并且需要psycopg2模块,请执行以下操作:

yum install postgresql-devel
easy_install psycopg2

如果您像我一样,可能会找不到有据可查的libpq-dev rpm。

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

yum install postgresql-devel
easy_install psycopg2

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


回答 7

我在Mac上遇到过同样的问题。

解决方案是仅使用PIP来安装所有东西,并触摸一些东西。

首先从以下 网址安装PIP:https //pip.pypa.io/en/latest/

然后,您要确定pg_config的路径是否在PATH中(回显$ PATH),如果没有,则可以编辑bash_profile:

vi /Users/<user>/.bash_profile

并添加以下行:

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

如果您不知道pg_config在哪里,则可以使用“ locate”工具,但是请确保您的locate.db是最新的(我使用的是旧的locate.db,并且使用的路径不存在)。

sudo /usr/libexec/locate.updatedb
locate pg_config

然后安装Django(如果需要)和psycopg2。

sudo pip install Django
sudo pip install psycopg2

然后在settings.py(localhost:defaultport)中

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

问候!

I was having the same Issue on Mac.

The solution was to use only PIP to install everything, and touch some things.

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

Then you want to make sure if path to pg_config is in your PATH (echo $PATH), if not you can edit your bash_profile:

vi /Users/<user>/.bash_profile

and add this line:

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

If you don’t know where pg_config is you can use the “locate” tool, but be sure your locate.db is up to date (i was using an old locate.db and using paths that does not exists).

sudo /usr/libexec/locate.updatedb
locate pg_config

Then install Django (if needed) 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': '',
    }
}

Greets!


回答 8

$ sudo apt-get install libpq-dev

年,这解决了我的问题。执行此操作后,请执行以下操作:pip install psycopg2

$ sudo apt-get install libpq-dev

Year, this solve my problem. After execute this, do: pip install psycopg2


回答 9

请注意,psycopg2通过pip或setup.py进行安装需要具有Visual Studio 2008(更确切地说是可执行文件vcvarsall.bat)。如果您没有管理员权限来安装它或在Windows上设置适当的PATH变量,则可以从此处下载已编译的库。

Please note that installation of psycopg2 via pip or setup.py requires to have Visual Studio 2008 (more precisely executable file vcvarsall.bat). If you don’t have admin rights to install it or set the appropriate PATH variable on Windows, you can download already compiled library from here.


回答 10

这是PostgreSQL在ubuntu服务器中设置的非常好的逐步过程之一。我已经尝试了它,Ubuntu 16.04并且可以正常工作。

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

This is one of the very good and step by step process to set up PostgreSQL in ubuntu server. I have tried it with Ubuntu 16.04 and its working.

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


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。