【问题标题】:Django - App Engine - Cloud SQL (PostgreSQL) - OperationalError: could not connect to server: Connection refusedDjango - App Engine - Cloud SQL (PostgreSQL) - OperationalError:无法连接到服务器:连接被拒绝
【发布时间】:2020-07-27 09:40:59
【问题描述】:

我已将 Django 部署到连接到 Cloud SQL (PostgreSQL) 实例的 App Engine。我不断收到以下错误:

OperationalError: could not connect to server: Connection timed out 

OperationalError: could not connect to server: Connection refused

app.yaml

# [START django_app]
runtime: python37
service: prestige-worldwide

handlers:
# This configures Google App Engine to serve the files in the app's static
# directory.
- url: /static
  static_dir: static/
# This handler routes all requests not caught above to your main app. It is
# required when static routes are defined, but can be omitted (along with
# the entire handlers section) when there are no static files defined.
- url: /.*
  script: auto
# [END django_app]

settings.py - 数据库配置

ALLOWED_HOSTS = ['app-engine url','127.0.0.1']

DATABASES = {
    'default': {'ENGINE': 'django.db.backends.postgresql_psycopg2',
                'NAME': 'postgres',
                'USER': 'postgres',
                'PASSWORD': 'admin',
                'HOST': 'instance public ip address',
                'PORT': 'ip address host',}}

它在本地使用 Cloud SQL 可以正常工作,但在部署到 App Engine 时无法正常工作。

【问题讨论】:

    标签: django postgresql google-app-engine google-cloud-sql


    【解决方案1】:

    像这样配置数据库解决了这个问题

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': 'postgres',
            'USER': 'postgres',
            'PASSWORD': 'admin',
        }
    }
    DATABASES['default']['HOST'] = '/cloudsql/instance-name'
    if os.getenv('GAE_INSTANCE'):
        pass
    else:
        DATABASES['default']['HOST'] = 'instance public ip address'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-12
      • 1970-01-01
      • 1970-01-01
      • 2013-05-19
      • 1970-01-01
      • 2020-05-29
      • 2020-08-19
      • 2017-04-28
      相关资源
      最近更新 更多