如何在Google App Engine上部署Django应用程序并与Postgres实例建立连接?

时间:2017-08-02 09:39:04

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

目前我正在尝试在Google App Engine(GAE)上部署Django应用。一切顺利,应用程序已部署,但部署后,它与Postgres实例的连接丢失。我不知道为什么会这样。以下是我的 settings.py 文件。

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'dbname',
        'USER': 'username',
        'PASSWORD': 'password',

    }
}
# In the flexible environment, you connect to CloudSQL using a unix socket.
# Locally, you can use the CloudSQL proxy to proxy a localhost connection
# to the instance
DATABASES['default']['HOST'] = '/cloudsql/shopnroar-175407:us-central1:snr-instance1'
if os.getenv('GAE_INSTANCE'):
    pass
else:
    DATABASES['default']['HOST'] = '100.107.126.241'

当我在本地运行时,它正在与谷歌云Postgres建立连接,因为我已经提供了ipv4地址来建立连接,但是一旦我在 GAE 上部署它,访问数据库时出现以下错误。

Is the server running locally and accepting
    connections on Unix domain socket "/cloudsql/shopnroar-175407:us-central1:snr-instance1/.s.PGSQL.5432"?

这是我的 app.yaml

# [START runtime]
runtime: python
env: flex
entrypoint: gunicorn -b :$PORT SNR.wsgi

env_variables:
    # Replace user, password, database, and instance connection name with the values obtained
    # when configuring your Cloud SQL instance.
    SQLALCHEMY_DATABASE_URI: >-
      postgresql+psycopg2://amad.uddin:goingtoin1122@/shopnroar?host=/cloudsql/shopnroar-175407:us-central1:snr-instance1
beta_settings:
    cloud_sql_instances: shopnroar-175407:us-central1:snr-instance1

runtime_config:
  python_version: 2
# [END runtime]

有人可以告诉我在GAE上部署django app后如何与postgres实例建立连接?

任何帮助或建议都将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

实际上 - 忘记我的旧答案 - 尝试将app.yaml值转换为字符串,这有助于我:

if WCSession.isSupported() {
    print("WCSession supported")
    let session = WCSession.default()
    session.delegate = self
    session.activate()
    if session.isReachable {
        session.sendMessage(["Instant":"Message"], replyHandler: nil, errorHandler: { error in
            print("Error sending message",error)
        })
    }
}