Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django-App Engine-Cloud SQL(PostgreSQL)-操作错误:无法连接到服务器:连接被拒绝_Django_Postgresql_Google App Engine_Google Cloud Sql - Fatal编程技术网

Django-App Engine-Cloud SQL(PostgreSQL)-操作错误:无法连接到服务器:连接被拒绝

Django-App Engine-Cloud SQL(PostgreSQL)-操作错误:无法连接到服务器:连接被拒绝,django,postgresql,google-app-engine,google-cloud-sql,Django,Postgresql,Google App Engine,Google Cloud Sql,我已将Django部署到连接到云SQL PostgreSQL实例的应用程序引擎。我不断发现以下错误: OperationalError: could not connect to server: Connection timed out 及 app.yaml # [START django_app] runtime: python37 service: prestige-worldwide handlers: # This configures Google App Engine to s

我已将Django部署到连接到云SQL PostgreSQL实例的应用程序引擎。我不断发现以下错误:

OperationalError: could not connect to server: Connection timed out 

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',}}

它可以在本地使用云SQL,但在部署到App Engine时不起作用。

这样配置DB解决了这个问题

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'
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'