2

I have 2 Postgres databases each in their own CloudSQL instance and a .NET web app running in GKE.

Goal: Connect web app utilizing EntityFramework Core to both CloudSQL instances using a single CloudSQL proxy.

I followed this setup and modified it following this S.O. answer.

There is an EF Core DbContext for each CloudSQL Instance. The context connections are set using 2 environment variables:

new Context1(
{
optionsBuilder.UseNpgsql(Environment.GetEnvironmentVariable("CONNECTION_1"));
});

new Context2(
{
optionsBuilder.UseNpgsql(Environment.GetEnvironmentVariable("CONNECTION_2"));
});

The environment variables are set as:

CONNECTION_1 = "Host=127.0.0.1;Port=5432;Database=postgres;Username=postgres;Password=password"

CONNECTION_2 = "Host=127.0.0.1;Port=5432;Database=postgres;Username=postgres;Password=password2"

Current Behavior:

Context1 interacts with CloudSQL instance1 as normal.

Context2 throws PostgresException "42P01: relation {my_Table_Name} does not exist." when trying to access a table.

Note: "my_Table_Name" is a table in CloudSQL instance2

This leads me to believe Context2 is trying to access CloudSQL instance1 instead of instance2.

How can I point Context2 through the SQL Proxy to CloudSQL instance2?

1 Answer 1

1

Basically this:

CONNECTION_1 = "Host=127.0.0.1;Port=5432;Database=postgres;Username=postgres;Password=password"
CONNECTION_2 = "Host=127.0.0.1;Port=5432;Database=postgres;Username=postgres;Password=password2"

means that you are connecting to the exact same Cloud SQL instance, with just two different passwords (same username). Not sure how CONNECTION_2 might even connect to the Cloud SQL instance1 though.

You would want to have something like this:

CONNECTION_1 = "Host=127.0.0.1;Port=5432;Database=postgres;Username=postgres;Password=password"
CONNECTION_2 = "Host=127.0.0.1;Port=5433;Database=postgres;Username=postgres;Password=password2"

and on your cloud-proxy command line (running on the same pod):

-instances=project:region:sqlinstance1=tcp:5432,project:region:sqlinstance2=tcp:5433
Sign up to request clarification or add additional context in comments.

3 Comments

Matching the ports to those defined in the deployment.yaml didn't work either. Any other ideas?
Try shelling into the pod and debug from there
@msauce4 did Cloud SQL Proxy worked for you in the end?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.