如何在 Django 中设置 PostgreSQL 数据库
我是 Python 和 Django 新手。
我正在使用 PostgreSQL 数据库引擎后端配置 Django 项目,但每个数据库操作都会出现错误。例如,当我运行manage.pysyncdb
时,我得到:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
您需要安装
psycopg2
Python 库。安装
下载http://initd.org/psycopg/,然后安装在Python PATH下
,下载后轻松解压tarball 和:
或者,如果您愿意,可以通过 easy_install 或 pip。
(无缘无故地我更喜欢使用 pip 而不是 easy_install。)
$ pip install psycopg2
配置
settings.py
- 其他安装说明可以在下载页面 和安装页面。
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:
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
- Other installation instructions can be found at download page and install page.
还要确保您已安装 PostgreSQL 开发包。
在 Ubuntu 上你需要做这样的事情:
Also make sure you have the PostgreSQL development package installed.
On Ubuntu you need to do something like this:
我使用的步骤:
您可能想要安装一个图形工具来管理您的数据库,为此您可以执行以下操作:
之后,您必须更改 Postgre 用户密码,然后执行以下操作:
在您的 settings.py 文件上执行以下操作:
额外:
如果你想使用命令行创建数据库,你可以这样做:
在你的settings.py文件上,你可以这样做:
Step by step that I use:
You may want to install a graphic tool to manage your databases, for that you can do:
After, you must change Postgre user password, then do:
On your settings.py file you do:
Extra:
If you want to create the db using the command line you can just do:
On your settings.py file you do:
您可以使用以下命令安装“psycopg”:
或者,您可以使用 pip :
easy_install 和 pip 包含在 ActivePython 中,或从 相应 项目 站点。
或者,只需获取预构建的 Windows 安装程序。
You can install "psycopg" with the following command:
Alternatively, you can use pip :
easy_install and pip are included with ActivePython, or manually installed from the respective project sites.
Or, simply get the pre-built Windows installer.
这可能看起来有点长,但它对我来说没有任何错误。
首先,从 Ubuntu 软件中心安装 phppgadmin。
然后在终端中运行这些步骤。
启动 apache 服务器
现在也在终端中运行它,以编辑 apache 文件。
将以下行添加到打开的文件中:
Now reload apache.使用终端。
现在您必须创建一个新数据库。以“postgres”用户身份登录。在终端中继续。
如果您对“postgres”的密码有疑问,可以使用此处的答案进行更改 https://stackoverflow.com/a /12721020/1990793 并继续执行这些步骤。
现在创建一个数据库
现在创建一个新用户以便稍后登录 phppgadmin,并提供新密码。
现在您的 postgressql 已设置完毕,您可以转到:
并使用您创建的新用户登录,以查看数据库。
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.
Start the apache server
Now run this too in terminal, to edit the apache file.
Add the following line to the opened file:
Now reload apache. Use terminal.
Now you will have to create a new database. Login as 'postgres' user. Continue in terminal.
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
Now create a new user to login to phppgadmin later, providing a new password.
Now your postgressql has been setup, and you can go to:
and login using the new user you've created, in order to view the database.
眼前的问题似乎是您缺少 psycopg2 模块。
The immediate problem seems to be that you're missing the psycopg2 module.
如果您使用的是 Fedora 20、Django 1.6.5、postgresql 9.3.* 并且您需要 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:
If you are like me, you may have trouble finding the well documented libpq-dev rpm... The above worked for me just now.
我在 Mac 上遇到了同样的问题。
解决方案是仅使用PIP来安装所有内容,并触摸一些内容。
首先从以下位置安装 PIP: https://pip.pypa.io/en/ latest/
然后你要确保 pg_config 的路径是否在你的 PATH 中(echo $PATH),如果没有,你可以编辑你的 bash_profile:
并添加这一行:
如果您不知道 pg_config 在哪里,您可以使用“locate”工具,但请确保您的locate.db是最新的(我使用的是旧的locate.db并使用不存在的路径)。
然后安装 Django(如果需要)和 psycopg2。
然后在 settings.py (localhost:defaultport) 中
问候!
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:
and add this line:
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).
Then install Django (if needed) and psycopg2.
And then in settings.py (localhost:defaultport)
Greets!
年,这解决了我的问题。
执行此操作后,执行以下操作:
pip 安装 psycopg2
Year, this solve my problem.
After execute this, do:
pip install psycopg2
从头开始使用 PostgreSQL 数据库创建 Django 应用程序的步骤。
检查您的系统中是否安装了 PostgreSQL。在 bash shell 中输入
<前><代码>psql --版本
如果安装了 PostgreSQL,则跳转到步骤 6。
安装 PostgreSQL -
默认情况下,PostgreSQL 安装会创建一个名为“postgres”的具有所有权限的特殊用户。登录 PostgreSQL
退出 PostgreSQL 会话
<前><代码>\q
现在为“postgres”用户设置密码。
登录 PostgreSQL -
在 PostgreSQL 交互式会话中输入 -
<前><代码>\密码 postgres
创建 PostgreSQL 数据库。
直接从 bash shell 创建 -
从 PostgreSQL 交互式会话创建 -
要检查数据库是否已创建,请使用 PostgreSQL 命令列出所有数据库
<前><代码>\l
如果您已有 Django 项目,则跳到步骤 11。否则创建虚拟环境。我使用 virtualenv 包装器。
创建 Django 项目。
Cd 到项目目录并创建您的应用程序。
安装 Django。
安装 Psycopg2。 Psycopg2 是 Python 的 PostgreSQL 数据库适配器。这是 Django 连接 PostgreSQL 所必需的。
更新settings.py如下
<前><代码>数据库 = {
'默认': {
'引擎': 'django.db.backends.postgresql_psycopg2',
'NAME': '你的数据库名称',
'用户':'postgres',
'密码': 'xxxxx',
'主机':'本地主机',
'港口': '',
}
}
运行迁移。
运行 Django 服务器并访问 localhost:8000
如果需要,您可以安装 pgadmin - 一个图形客户端来查看和操作 PostgreSQL 数据库架构和数据。
Steps to create a Django app with PostgreSQL database from scratch.
Check if PostgreSQL is installed in your system. In your bash shell enter
If PostgreSQL is installed jump to step 6.
To install PostgreSQL -
By default PostgreSQL installation creates a special user named "postgres" that has all rights. To login to PostgreSQL
To exit PostgreSQL session
Now set password for "postgres" user.
Login to PostgreSQL -
In PostgreSQL interactive session enter -
Create PostgreSQL database.
To create directly from bash shell -
To create from PostgreSQL interactive session -
To check if the database is created, list all databases using PostgreSQL command
If you already have an existing Django project then jump to step 11. Otherwise create a virtual environment. I use virtualenv wrapper.
Create Django project.
Cd to the project directory and create your app.
Install Django.
Install Psycopg2. Psycopg2 is a PostgreSQL database adapter for Python. This is required for Django to connect with PostgreSQL.
Update settings.py as follows
Run migration.
Run Django server and visit localhost:8000
If required you can install pgadmin - a graphical client to view and manipulate PostgreSQL database schema and data.
请注意,通过 pip 或 setup.py 安装 psycopg2 需要有 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.这是在 ubuntu 服务器中设置 PostgreSQL 的非常好的分步过程之一。我已经在
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 withUbuntu 16.04
and its working.https://www.digitalocean.com/community/tutorials/how-to-use-postgresql-with-your-django-application-on-ubuntu-14-04