简体   繁体   English

使用 Cloud SQL PostgreSQL DB 在 Cloud Run 上的 Django 应用程序出错 - 无法连接到数据库

[英]Error with Django application on Cloud Run using Cloud SQL PostgreSQL DB - cannot connect to database

I have this Django project on Google Cloud on Cloud Run.我在 Cloud Run 上的 Google Cloud 上有这个 Django 项目。 It is supposed to connect to the Cloud SQL PostgreSQL database and work, but it doesn't.应该连接到云 SQL PostgreSQL 数据库并工作,但它没有。 Here is the database part of settings.py (I have removed the private data):这是settings.py的数据库部分(我已经删除了私有数据):

DATABASES = {
  'default': {   
      'ENGINE': 'django.db.backends.postgresql_psycopg2',
      'NAME': 'db',
      'USER': 'user',
      'PASSWORD': 'password',
      'HOST': '/cloudsql/project:europe-west4:instance',
      'PORT': '5432',
  }
}

However, when I try to do anything database-related (for example, log in), it throws the error但是,当我尝试做任何与数据库相关的事情(例如,登录)时,它会抛出错误

OperationalError at /accounts/login/
could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/cloudsql/*project*:europe-west4:*instance*/.s.PGSQL.5432"?

If I remove the /cloudsql/ from HOST it gives the error如果我从 HOST 中删除/cloudsql/它会给出错误

OperationalError at /accounts/login/
could not translate host name "*project*:europe-west4:*instance*" to address: Name or service not known

When I use the Cloud SQL proxy exe to test on my local computer it works.当我使用 Cloud SQL 代理 exe 在我的本地计算机上进行测试时,它可以工作。 However, when I deploy it to Cloud Run it doesn't.但是,当我将它部署到 Cloud Run 时,它不会。

Have you enabled connecting to that Cloud SQL instance from Cloud Run?您是否已启用从 Cloud Run 连接到该 Cloud SQL 实例?

Try running尝试运行

gcloud run services update [your cloud run service name] \
  --add-cloudsql-instances [project:region:instance]

Also check that your Cloud Run service account has Cloud SQL Client permissions.还要检查您的 Cloud Run 服务帐户是否具有Cloud SQL Client权限。 See this documentation for more information on how to do that.有关如何执行此操作的更多信息,请参阅此文档

Try using the full url in host parameter尝试在host参数中使用完整的 url

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用云运行通过 django 中的云代理连接到云 sql 实例时出错 - Error while connecting to a cloud sql instance via cloud proxy in django using cloud run Django 连接到 IBM Cloud 数据库 - Django connect to IBM Cloud database 将Django连接到Google Cloud SQL - Connect Django to Google Cloud SQL Google App Engine 上的 Django 连接到云 SQL,无法连接 - Django on Google App Engine connection to Cloud SQL, cannot connect 无法将 Django Rest 框架应用程序与 GCP 云 SQL 连接 - Unable to connect Django Rest framework application with GCP cloud SQL 将 Django 连接到 Google Cloud 计算引擎中的 SQL 数据库 - Connect Django to SQL database in Google Cloud's Compute Engine Google Cloud run 无法访问 Cloud SQL - Google cloud run cannot reach Cloud SQL Google App Engine / Cloud SQL / Django syncdb数据库后端错误 - Google App Engine/Cloud SQL/Django syncdb database backend error Django - App Engine - 云 SQL (PostgreSQL) - 操作错误:无法连接到服务器:连接被拒绝 - Django - App Engine - Cloud SQL (PostgreSQL) - OperationalError: could not connect to server: Connection refused 将django连接到Google Cloud SQL Second Generation - Connect django to Google Cloud SQL Second Generation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM