小编Mic*_*ris的帖子

无法从使用自定义运行时在 Docker 中运行 .NET Core 应用程序的 App Engine 连接到 Google Cloud SQL 中的 postgres

Exception while connecting我尝试了很多方法,但在尝试从我的 Google Cloud App Engine 连接到 Google Cloud PostgreSQL 实例时似乎无法通过。

这可能是我作为开发人员处理过的最令人沮丧的事情。连接到数据库不应该这么困难。

我究竟做错了什么?

不起作用的事情:

应用程序.yaml:

runtime: custom
env: flex
beta_settings:
  cloud_sql_instances: "<project-id>:<region>:<sql-instance>=tcp:5432"
Run Code Online (Sandbox Code Playgroud)

最终使用的连接字符串:

Uid=<db_user>;Pwd=<db_password>;Host=cloudsql;Database=<db_name>

// other attempts:
Uid=<db_user>;Pwd=<db_password>;Host=cloudsql;Database=<db_name>;Port=5432
Uid=<db_user>;Pwd=<db_password>;Host=/cloudsql/<project-id>:<region>:<sql-instance>;Database=<db_name>
Uid=<db_user>;Pwd=<db_password>;Host='/cloudsql/<project-id>:<region>:<sql-instance>';Database=<db_name>
Uid=<db_user>;Pwd=<db_password>;Server='/cloudsql/<project-id>:<region>:<sql-instance>';Database=<db_name>
Run Code Online (Sandbox Code Playgroud)

用法:

var connectionString = new NpgsqlConnectionStringBuilder(<connection string>)
{
    SslMode = SslMode.Disable
};

NpgsqlConnection connection =
    new NpgsqlConnection(connectionString.ConnectionString);

connection.Open();
Run Code Online (Sandbox Code Playgroud)

相关堆栈跟踪:

Exception while connecting
   at Npgsql.NpgsqlConnector.Connect(NpgsqlTimeout timeout)
   at Npgsql.NpgsqlConnector.RawOpen(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlConnector.Open(NpgsqlTimeout …
Run Code Online (Sandbox Code Playgroud)

c# postgresql google-app-engine google-cloud-sql docker

7
推荐指数
1
解决办法
2690
查看次数