如何在 Django 中设置 PostgreSQL 数据库

发布于 2024-10-25 05:05:34 字数 2195 浏览 2 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

送舟行 2024-11-01 05:05:34

您需要安装 psycopg2 Python 库。

安装


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

,下载后轻松解压tarball 和:

$ python setup.py install

或者,如果您愿意,可以通过 easy_installpip

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

  • $ easy_install psycopg2
  • $ pip install psycopg2

配置


settings.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.

许一世地老天荒 2024-11-01 05:05:34

还要确保您已安装 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
与之呼应 2024-11-01 05:05:34

我使用的步骤:

 - 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 pgadmin4 

之后,您必须更改 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 pgadmin4 

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': '',
    }
}
夜巴黎 2024-11-01 05:05:34

您可以使用以下命令安装“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.

江心雾 2024-11-01 05:05:34

这可能看起来有点长,但它对我来说没有任何错误。

首先,从 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

Now reload 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.

蓝戈者 2024-11-01 05:05:34

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

The immediate problem seems to be that you're missing the psycopg2 module.

霊感 2024-11-01 05:05:34

如果您使用的是 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.

清风夜微凉 2024-11-01 05:05:34

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

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

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

然后你要确保 pg_config 的路径是否在你的 PATH 中(echo $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!

╭⌒浅淡时光〆 2024-11-01 05:05:34
$ sudo apt-get install libpq-dev

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

$ sudo apt-get install libpq-dev

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

千柳 2024-11-01 05:05:34

从头开始使用 PostgreSQL 数据库创建 Django 应用程序的步骤。

  1. 检查您的系统中是否安装了 PostgreSQL。在 bash shell 中输入

    <前><代码>psql --版本

  2. 如果安装了 PostgreSQL,则跳转到步骤 6。

  3. 安装 PostgreSQL -

    sudo apt-get install python3-dev libpq-dev
    
    sudo apt-get install postgresql postgresql-contrib
    
  4. 默认情况下,PostgreSQL 安装会创建一个名为“postgres”的具有所有权限的特殊用户。登录 PostgreSQL

    sudo -u postgres psql
    

    退出 PostgreSQL 会话

    <前><代码>\q

  5. 现在为“postgres”用户设置密码。

    登录 PostgreSQL -

    sudo -u postgres psql
    

    在 PostgreSQL 交互式会话中输入 -

    <前><代码>\密码 postgres

  6. 创建 PostgreSQL 数据库。

    直接从 bash shell 创建 -

    sudo -u postgres createdb your_db_name
    

    从 PostgreSQL 交互式会话创建 -

    createdb your_db_name
    

    要检查数据库是否已创建,请使用 PostgreSQL 命令列出所有数据库

    <前><代码>\l

  7. 如果您已有 Django 项目,则跳到步骤 11。否则创建虚拟环境。我使用 virtualenv 包装器。

    mkvirtualenv your_project_name
    
  8. 创建 Django 项目。

    django-admin startproject your_project_name
    
  9. Cd 到项目目录并创建您的应用程序。

     python manage.py startapp your_app_name
    
  10. 安装 Django。

    pip 安装 django
    
  11. 安装 Psycopg2。 Psycopg2 是 Python 的 PostgreSQL 数据库适配器。这是 Django 连接 PostgreSQL 所必需的。

    pip install psycopg2
    
  12. 更新settings.py如下

    <前><代码>数据库 = {
    '默认': {
    '引擎': 'django.db.backends.postgresql_psycopg2',
    'NAME': '你的数据库名称',
    '用户':'postgres',
    '密码': 'xxxxx',
    '主机':'本地主机',
    '港口': '',
    }
    }

  13. 运行迁移。

    python3 管理.py 迁移
    
  14. 运行 Django 服务器并访问 localhost:8000

    python3 manage.py runserver 0.0.0.0:8000
    
  15. 如果需要,您可以安装 pgadmin - 一个图形客户端来查看和操作 PostgreSQL 数据库架构和数据。

Steps to create a Django app with PostgreSQL database from scratch.

  1. Check if PostgreSQL is installed in your system. In your bash shell enter

    psql --version
    
  2. If PostgreSQL is installed jump to step 6.

  3. To install PostgreSQL -

    sudo apt-get install python3-dev libpq-dev
    
    sudo apt-get install postgresql postgresql-contrib
    
  4. By default PostgreSQL installation creates a special user named "postgres" that has all rights. To login to PostgreSQL

    sudo -u postgres psql
    

    To exit PostgreSQL session

    \q
    
  5. Now set password for "postgres" user.

    Login to PostgreSQL -

    sudo -u postgres psql
    

    In PostgreSQL interactive session enter -

    \password postgres
    
  6. Create PostgreSQL database.

    To create directly from bash shell -

    sudo -u postgres createdb your_db_name
    

    To create from PostgreSQL interactive session -

    createdb your_db_name
    

    To check if the database is created, list all databases using PostgreSQL command

    \l
    
  7. If you already have an existing Django project then jump to step 11. Otherwise create a virtual environment. I use virtualenv wrapper.

    mkvirtualenv your_project_name
    
  8. Create Django project.

    django-admin startproject your_project_name
    
  9. Cd to the project directory and create your app.

     python manage.py startapp your_app_name
    
  10. Install Django.

    pip install django
    
  11. Install Psycopg2. Psycopg2 is a PostgreSQL database adapter for Python. This is required for Django to connect with PostgreSQL.

    pip install psycopg2
    
  12. Update settings.py as follows

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': 'your_db_name',
            'USER': 'postgres',
            'PASSWORD': 'xxxxx',
            'HOST': 'localhost',
            'PORT': '',
        }
    }
    
  13. Run migration.

    python3 manage.py migrate
    
  14. Run Django server and visit localhost:8000

    python3 manage.py runserver 0.0.0.0:8000
    
  15. If required you can install pgadmin - a graphical client to view and manipulate PostgreSQL database schema and data.

少年亿悲伤 2024-11-01 05:05:34

请注意,通过 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.

乖不如嘢 2024-11-01 05:05:34

这是在 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 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文