连接Postgresql到Django

huangapple go评论108阅读模式
英文:

Connecting Postgresql to Django

问题

  1. 我正在尝试将两个数据库连接到Django项目,以便我可以在开发时使用一个,而在生产时使用另一个。
  2. 我的设置文件:
  3. ```python
  4. DATABASES = {
  5. 'default': {
  6. 'ENGINE': 'django.db.backends.sqlite3',
  7. 'NAME': BASE_DIR / 'db.sqlite3',
  8. }
  9. }
  10. db_from_env = dj_database_url.config(conn_max_age=500)
  11. DATABASES["default"].update(db_from_env)
  12. export DATABASE_URL=path_to_database

当我尝试运行服务器时,我收到了这个错误:“django.db.utils.NotSupportedError: 需要 PostgreSQL 12 或更高版本(发现 11.18)”。

但当我在终端中输入psql时,我得到:psql(14.7(Homebrew),服务器 15.2)。
而我在我的Mac上未安装 PostgrSQL@11。

  1. <details>
  2. <summary>英文:</summary>
  3. I am trying to connect 2 databases to django project so I can use one for develop and another on production
  4. My setting file

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}

db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES["default"].update(db_from_env)

export DATABASE_URL=path_to_database

  1. When i try to run server I got this error: &quot;django.db.utils.NotSupportedError: PostgreSQL 12 or later is required (found 11.18).&quot;
  2. But when i write psql in terminal I got: psql (14.7 (Homebrew), server 15.2)
  3. And PostgrSQL@11 is not installed on my mac
  4. </details>
  5. # 答案1
  6. **得分**: 1
  7. 即使`psql`客户端连接到更新版本的`PostgreSQL`服务器,你的机器上可能还有其他运行中的`PostgreSQL`服务器。
  8. 你可以通过运行 `ps auxwww | grep postgres` 来找到它们。
  9. 每个运行中的服务器都在不同的端口上监听。因为`psql`客户端正在连接到`PostgreSQL@14.7`,这个服务器可能在端口5432上运行(这是`psql`默认连接的端口)。
  10. 检查你的Django环境配置中的端口号 - 可能Django正在尝试连接到监听在不同端口上的`PostgreSQL`服务器。
  11. <details>
  12. <summary>英文:</summary>
  13. Even if `psql` client is connected to a newer version of `PostgreSQL` server, there might be additional `PostgreSQL` servers running on your machine.
  14. You can find them by running `ps auxwww | grep postgres`.
  15. Each running server listens on a distinct port number. Because `psql` client is connecting to `PostgreSQL@14.7`, this server is probably running on the port 5432 (that is the default port `psql` is connecting to).
  16. Check your Django env config for the port number - probably Django is trying to connect to `PostgreSQL` server listening on a different port.
  17. </details>

huangapple
  • 本文由 发表于 2023年4月20日 06:43:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059314.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定