Django无法连接到RDS PostgreSQL[英] django cannot connect to RDS postgresql

本文是小编为大家收集整理的关于Django无法连接到RDS PostgreSQL的处理方法,想解了Django无法连接到RDS PostgreSQL的问题怎么解决?Django无法连接到RDS PostgreSQL问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

我的Django应用程序部署到EC2时无法连接到RDS PostgreSQL.但奇怪的是,它在我的台式机中运行时可以正常工作.

EC2服务器和桌面配置了Python3,django1.9,apache2和mod_wsgi_py3

这是我的settings.py数据库设置:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'some_name',
        'PASSWORD': 'some_password',
        'HOST': 'myhostname.cltlezrr85xn.ap-northeast-1.rds.amazonaws.com',
        'PORT': 5432,
    }
}

和error.log from apache2:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py", line 199, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py", line 171, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/postgresql/base.py", line 175, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/usr/local/lib/python3.4/dist-packages/psycopg2/__init__.py", line 164, in connect
    conn = _connect(dsn, connection_factory=connection_factory, async=async)
psycopg2.OperationalError: could not connect to server: Connection timed out
    Is the server running on host "myhostname.cltlezrr85xn.ap-northeast-1.rds.amazonaws.com" (172.--.--.---) and accepting
    TCP/IP connections on port 5432?


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.4/dist-packages/django/contrib/auth/management/commands/createsuperuser.py", line 52, in execute
    return super(Command, self).execute(*args, **options)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.4/dist-packages/django/contrib/auth/management/commands/createsuperuser.py", line 86, in handle
    default_username = get_default_username()
  File "/usr/local/lib/python3.4/dist-packages/django/contrib/auth/management/__init__.py", line 189, in get_default_username
    auth_app.User._default_manager.get(username=default_username)
  File "/usr/local/lib/python3.4/dist-packages/django/db/models/manager.py", line 122, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/django/db/models/query.py", line 381, in get
    num = len(clone)
  File "/usr/local/lib/python3.4/dist-packages/django/db/models/query.py", line 240, in __len__
    self._fetch_all()
  File "/usr/local/lib/python3.4/dist-packages/django/db/models/query.py", line 1074, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/usr/local/lib/python3.4/dist-packages/django/db/models/query.py", line 52, in __iter__
    results = compiler.execute_sql()
  File "/usr/local/lib/python3.4/dist-packages/django/db/models/sql/compiler.py", line 846, in execute_sql
    cursor = self.connection.cursor()
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py", line 231, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py", line 204, in _cursor
    self.ensure_connection()
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py", line 199, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.4/dist-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python3.4/dist-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py", line 199, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py", line 171, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/postgresql/base.py", line 175, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/usr/local/lib/python3.4/dist-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 timed out
    Is the server running on host "myhostname.cltlezrr85xn.ap-northeast-1.rds.amazonaws.com" (172.--.--.---) and accepting
    TCP/IP connections on port 5432?

感谢您的建议..

推荐答案

编辑:

再次浏览了这一点,并具有更轻松的方法.

当您尝试使用EC2或EB上的Django使用现有的RDS数据库时,您必须调整安全组,然后获取正确的参数并将它们设置为环境变量(RDS _*)

1)创建RD,并匹配这些:

Environment variables - RDS console label

RDS_HOSTNAME - Endpoint (this is the hostname)
RDS_PORT - Port
RDS_DB_NAME –  DB Name
RDS_USERNAME –  Username
RDS_PASSWORD – Password you set for your DB

2)设置使用的设置,例如eb setenv

3)转到您的EC2/EB实例,并获得该安全组,例如awseb-z-afsafdsaf-stack-AWSEBSecurityGroup-asfdsadfasdf

4)转到您的RDS实例的面板,向下滚动到安全组,并注意其具有哪个安全组.例如rds-launch-wizard-1 (ab-sdjfalkajsdf39)

5)选择RDS安全组,并添加一个类型:PostgreSQL(或您正在使用的任何DB),以及使用您在步骤2中作为源的EC2或EB实例(> ).协议和端口范围应自动填充.

6)保存

就是这样.


原始:

对于任何偶然发现这个问题的人:

当您尝试使用EC2或EB上的Django使用现有的RDS数据库时,您必须调整安全组,然后获取正确的参数并将它们设置为环境变量(RDS _*)

1)创建RD,并匹配这些:

Environment variables - RDS console label

RDS_HOSTNAME - Endpoint (this is the hostname)
RDS_PORT - Port
RDS_DB_NAME –  DB Name
RDS_USERNAME –  Username
RDS_PASSWORD – Password you set for your DB

2)设置使用的设置,例如eb setenv

3)转到您的EC2/EB实例,并获得该安全组,例如awseb-z-afsafdsaf-stack-AWSEBSecurityGroup-asfdsadfasdf和负载平衡器:awseb-e-adsfadsf-stack-AWSEBLoadBalancerSecurityGroup-asdfadsf

4)转到您的RDS实例的面板,向下滚动到安全组,并注意其具有哪个安全组.例如rds-launch-wizard-1 (ab-sdjfalkajsdf39)

5)单击" RDS实例"的修改,在中间设置的安全组中,添加您上面发现的负载平衡器安全组.它应该有建议.

6)转到EC2仪表板,然后从左侧的菜单中选择安全组.

7)选择负载平衡器安全组,然后添加出站规则.类型应为您的RDS类型(PostgreSQL),并且目标应该是自定义和RDS实例的安全组.保存.

8)对入站执行相同的操作,请使用相同的RDS类型和目标

9)选择RDS安全组,并添加一个类似于8的入站规则,但使用您在步骤2中获得的EC2或EB实例.

.

10)保存,您完成了.他们现在应该能够一起工作.

我不是100%确定所有这些步骤都是必要的,一个或两个可能不是,但这为我完成了.

本文地址:https://www.itbaoku.cn/post/1764193.html