gpt4 book ai didi

python - Django 和 postgres : Not connecting

转载 作者:行者123 更新时间:2023-11-28 21:52:58 24 4
gpt4 key购买 nike

我一直在尝试将 Django 与 postgress 一起使用,但似乎我遇到了某种连接问题。

看来我的问题与 settings.py 文件有关,其中提供了与数据库交互的这些设置:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydb',
'USER': 'myuser',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '8000',
}
}

当我从 settings.py 中删除上面的这些行时,

python manage.py runserver 

按预期运行服务器。另一方面,当出现这些行时,我会收到消息:

conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 8000?

这个问题本来可以预料到的,因为它是在我尝试“迁移”时首先出现的

python manage.py migrate

如下所示,我的用户具有以下属性:

Role name |                   Attributes                   | Member of | Description 
-----------+------------------------------------------------+-----------+-------------
myuser | Superuser, Create DB | {} |
postgres | Superuser, Create role, Create DB, Replication | {} |

最后,我似乎对 psycopg2 的安装没有任何问题,因为我能够创建如下所示的 mydb。事实上,我系统的数据库列表是

                                  List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
mydb | myuser | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)

我应该包含新属性吗?哪个?还有别的吗?我是 Django 和 postgres 的新手,非常欢迎任何帮助。

顺便说一下,我使用的是 Ubuntu 14.04、Python 3.4 和 Django 1.7.1。

最佳答案

我会检查 postgresql.conf 以确认 PostgreSQL 正在用来监听的端口。默认值通常是 5432,而不是 8000,所以除非您明确配置 PostgreSQL 使用 8000,否则我怀疑这至少是您问题的一部分,并且错误消息肯定会由此解释(尽管它可能不是唯一可能的原因).

关于python - Django 和 postgres : Not connecting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27469018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com