Django没有连接到Elastic beanstalk上的PostGres

时间:2015-06-08 05:05:48

标签: django postgresql amazon-web-services amazon-ec2

我在AWS EC2上使用Elastic Beanstalk设置了django。但是,尝试运行该应用程序会在日志文件中出现以下错误

File "/opt/python/run/venv/local/lib/python2.7/site-packages/psycopg2/__init__.py", line 164, in connect
  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 "localhost" (127.0.0.1) and accepting
  TCP/IP connections on port 5432?

   (ElasticBeanstalk::ExternalInvocationError)


[2015-06-08T04:52:45.155Z] INFO  [5434]  - [CMD-AppDeploy/AppDeployStage0/EbExtensionPostBuild/Infra-EmbeddedPostBuild/postbuild_0_SportsGullyRest/Command 01_migrate] : Activity failed.
[2015-06-08T04:52:45.155Z] INFO  [5434]  - [CMD-AppDeploy/AppDeployStage0/EbExtensionPostBuild/Infra-EmbeddedPostBuild/postbuild_0_SportsGullyRest] : Activity failed.
[2015-06-08T04:52:45.155Z] INFO  [5434]  - [CMD-AppDeploy/AppDeployStage0/EbExtensionPostBuild/Infra-EmbeddedPostBuild] : Activity failed.
[2015-06-08T04:52:45.191Z] INFO  [5434]  - [CMD-AppDeploy/AppDeployStage0/EbExtensionPostBuild] : Activity failed.
[2015-06-08T04:52:45.191Z] INFO  [5434]  - [CMD-AppDeploy/AppDeployStage0] : Activity failed.
[2015-06-08T04:52:45.191Z] INFO  [5434]  - [CMD-AppDeploy] : Completed activity. Result:
  Command CMD-AppDeploy failed.
[2015-06-08T04:58:30.553Z] INFO  [5642]  - [CMD-BundleLogs] : Starting activity...
[2015-06-08T04:58:30.781Z] INFO  [5642]  - [CMD-BundleLogs/AddonsBefore] : Starting activity...
[2015-06-08T04:58:30.781Z] INFO  [5642]  - [CMD-BundleLogs/AddonsBefore] : Completed activity.
[2015-06-08T04:58:30.781Z] INFO  [5642]  - [CMD-BundleLogs/BundleLogs] : Starting activity...
[2015-06-08T04:58:30.782Z] INFO  [5642]  - [CMD-BundleLogs/BundleLogs/BundleLogs] : Starting activity...

我的Python配置文件是:

option_settings:
  "aws:elasticbeanstalk:application:environment":
    DJANGO_SETTINGS_MODULE: "SportsGullyRest.settings"
    "PYTHONPATH": "/opt/python/current/app/SportsGullyRest:$PYTHONPATH"
    "ALLOWED_HOSTS": ".elasticbeanstalk.com"
  "aws:elasticbeanstalk:container:python":
    WSGIPath: SportsGullyRest/wsgi.py
    NumProcesses: 3
    NumThreads: 20
  "aws:elasticbeanstalk:container:python:staticfiles":
    "/static/": "/static/"

我是否缺少部署应用程序的东西?

编辑:

这是我的DATABASES的settings.py

if 'RDS_DB_NAME' in os.environ:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': os.environ['RDS_DB_NAME'],
            'USER': os.environ['RDS_USERNAME'],
            'PASSWORD': os.environ['RDS_PASSWORD'],
            'HOST': os.environ['RDS_HOSTNAME'],
            'PORT': os.environ['RDS_PORT'],
        }
    }
else:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': 'dbname',
            'USER': 'sgadmin',
            'PASSWORD': 'root',
            'HOST': 'localhost',
            'PORT': '5432',
        }
    }

1 个答案:

答案 0 :(得分:0)

问题可能是以下任何一种情况:

  1. 您是否在您的环境中设置了postgres?
  2. 它在运行吗?
  3. 你的实例是否突然关闭而你必须重启EC2?检查服务状态
  4. 我通常遇到第三个问题,它也导致了一次损坏的DB文件。我不得不重新进行设置。我建议你转移到RDS,因为它会为你提供更多稳定性,否则你必须自己负责管理postgres数据库。