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

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

<p>I followed <a href="https://github.com/GoogleCloudPlatform/kubernetes-engine-samples/blob/master/cloudsql/postgres_deployment.yaml" rel="nofollow noreferrer">this</a> setup and modified it following <a href="https://stackoverflow.com/questions/40793222/connecting-to-multiple-cloudsql-instances-using-cloud-sql-proxy">this</a> S.O. answer.</p>

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

<pre><code>new Context1(
{
optionsBuilder.UseNpgsql(Environment.GetEnvironmentVariable("CONNECTION_1"));
});

new Context2(
{
optionsBuilder.UseNpgsql(Environment.GetEnvironmentVariable("CONNECTION_2"));
});
</code></pre>

<p>The environment variables are set as:</p>

<pre><code>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"
</code></pre>

<p><strong>Current Behavior:</strong></p>

<p>Context1 interacts with CloudSQL instance1 as normal.</p>

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

<p>Note: <code>"my_Table_Name"</code> is a table in CloudSQL instance2</p>

<p>This leads me to believe Context2 is trying to access CloudSQL instance1 instead of instance2.</p>

<p>How can I point Context2 through the SQL Proxy to CloudSQL instance2?</p>

msauce4
<p>Basically this:</p>

<blockquote>
<pre><code>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"
</code></pre>
</blockquote>

<p>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.</p>

<p>You would want to have something like this:</p>

<blockquote>
<pre><code>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"
</code></pre>
</blockquote>

<p>and on your cloud-proxy command line (running on the same pod):</p>

<pre><code>-instances=project:region:sqlinstance1=tcp:5432,project:region:sqlinstance2=tcp:5433
</code></pre>
