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?
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