I've tried numerous things, and can't seem to get past an Exception while connecting
when trying to connect to a Google Cloud PostgreSQL instance from my Google Cloud App Engine.
This is probably the most frustrating thing I've ever dealt with as a developer. It shouldn't be this difficult to connect to a database.
What am I doing wrong?
Things that didn't work:
- How to connect Google Cloud SQL via Google App engine Flex to botnet core app?
- Unable to connect to postgres in cloud sql from google app engine nodes
- https://cloud.google.com/sql/docs/postgres/connect-app-engine-flexible
- https://github.com/GoogleCloudPlatform/dotnet-docs-samples/tree/master/appengine/flexible/CloudSql
app.yaml:
runtime: custom
env: flex
beta_settings:
cloud_sql_instances: "<project-id>:<region>:<sql-instance>=tcp:5432"
eventual connection string used:
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>
Usage:
var connectionString = new NpgsqlConnectionStringBuilder(<connection string>)
{
SslMode = SslMode.Disable
};
NpgsqlConnection connection =
new NpgsqlConnection(connectionString.ConnectionString);
connection.Open();
Relevant Stack Trace:
Exception while connecting
at Npgsql.NpgsqlConnector.Connect(NpgsqlTimeout timeout)
at Npgsql.NpgsqlConnector.RawOpen(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
at Npgsql.ConnectorPool.AllocateLong(NpgsqlConnection conn, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlConnection.<>c__DisplayClass32_0.<g__OpenLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Npgsql.NpgsqlConnection.Open()
Dockerfile (maybe it matters since I'm using custom
in my app.yaml
runtime
?):
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 8080
EXPOSE 443
ENV ASPNETCORE_URLS=http://*:8080
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
COPY . /src
WORKDIR /src
RUN dotnet restore --packages /packages
RUN dotnet publish -c Release -o /published
FROM base AS final
COPY --from=build /published /app/
WORKDIR /app
ENTRYPOINT [ "dotnet", "myapp.dll" ]
Deploy script:
gcloud beta app deploy --project <project-id>
EDIT:
Checking the PostgreSQL errors from the cloud console for my sql instance, I see the following error just before the final Exception while connecting
error:
System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (99): Cannot assign requested address /cloudsql/<my connection instance id>/.s.PGSQL.5432
Cloud SQL Client
role assigned to every account listed in my IAM listing. I've also tried with theCloud SQL Admin
role. Finally, hoping that it was an issue of using thecustom
runtime instead ofaspnetcore
runtime in myapp.yaml
, I also changed it to useaspnetcore
, but still saw the same result. – Rumrunner