簡體   English   中英

Django - App Engine - 雲 SQL (PostgreSQL) - 操作錯誤:無法連接到服務器:連接被拒絕

[英]Django - App Engine - Cloud SQL (PostgreSQL) - OperationalError: could not connect to server: Connection refused

我已將 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 時不起作用。

像這樣配置數據庫解決了這個問題

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'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM