【问题标题】:while deplyoing django app on heroku postgres doesn't work虽然在heroku postgres上部署django应用程序不起作用
【发布时间】:2015-12-23 12:28:37
【问题描述】:

我正在尝试在 heroku 服务器上部署我的 django 应用程序,我按照该网站 https://devcenter.heroku.com/articles/getting-started-with-python#introduction 的说明进行操作。它运行良好,直到“heroku open”命令。当我来到需要托管我的数据库的部分时使用“heroku run python manage.py syncdb”命令,它无法显示消息“OperationalError:无法连接到服务器:连接被拒绝 服务器是否在主机“localhost”(127.0.0.1)上运行并接受 端口 5432 上的 TCP/IP 连接?”。我尝试了很多修复,包括此处建议的修复 Deploying Django app's local postgres database to heroku?http://www.cyberciti.biz/tips/postgres-allow-remote-access-tcp-connection.html。我尝试了所有解决方案,包括编辑“listen_address”='*' 和 tcpip_socket='true' postgresql.conf 并将 pg_hba.conf 中的 ipv4 和 v6 值编辑为 托管所有所有 127.0.0.1 255.255.0.1 信任 托管所有 10.0.0.99/32 md5 托管所有所有 0.0.0.0/0 .

但是它们都不起作用。我猜是因为 heroku 无法连接到我的本地 postgres 服务器。这很奇怪,因为我可以通过 pgadmin 访问 postgres 服务器。

而且在 django settings.py 中也是这样的

数据库 = { '默认': { '引擎': 'django.db.backends.postgresql_psycopg2', 'NAME': 'django_test', '用户':'postgres', '密码': '******', 'HOST': 'localhost', # 或者你的数据库所在的 IP 地址 '端口':'5432', } } 我需要改变这个并使用heroku的数据库设置吗??

【问题讨论】:

  • 所以 postgres 服务器位于 heroku 服务器上,对吗?
  • 我的本地系统上没有
  • 但是如果我给我在heroku上创建的postgres db的服务器url(以“postgres//xxxxxxxxxxxxxx”开头,它说不能翻译地址

标签: django postgresql heroku django-database django-deployment


【解决方案1】:

服务器上的localhost 指向服务器而不是您的本地计算机。原因是因为运行您的 django 代码的服务器将尝试解析 dns 名称 localhost 并且它有一个指向 127.0.0.1 的指针,该指针对于解析该名称的服务器来说是本地的。这不会指向您正在使用的计算机。

您需要在 heroku 上获取 postgres 实例,并在 django 设置中将 HOST: 'xxx.xxx.xxx.xxx 更改为新 postgres 实例的 IP 地址。

【讨论】:

  • 如果我系统上的配置显示这个 eth0 Link encap:Ethernet HWaddr fc:aa:14:5f:7b:84 inet addr:192.168.0.101 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr : fe80::feaa:14ff:fe5f:7b84/64 范围:链接 UP BROADCAST RUNNING MULTICAST MTU:1500 指标:1 RX 数据包:332469 错误:0 丢弃:2 超限:0 帧:0 TX 数据包:287358 错误:0 丢弃:0 超限:0 运营商:0 冲突:0 txqueuelen:1000 “我认为其中不包含我的 IP 地址”
  • 不,它没有。在 heroku 上启动 postgres 实例或将 postgres 添加到您当前的 heroku 服务器会更容易。
  • VOILA,它工作了,谢谢伙计,我使用的是 postgres 的 url,而不是主机......非常感谢
猜你喜欢
  • 2011-12-05
  • 1970-01-01
  • 2017-01-12
  • 1970-01-01
  • 2021-10-28
  • 2018-05-15
  • 2013-08-22
  • 1970-01-01
  • 2015-06-30
相关资源
最近更新 更多