title: How to connect to AlloyDB using Auth Proxy and Python?
body: <p>I am trying to connect to the AlloyDB in google cloud via alloydb-auth-proxy. I am not able to connect to it successfully. I am getting error while trying to do this.</p>
  <p>I followed the instruction mentioned in <a href="https://cloud.google.com/alloydb/docs/auth-proxy/connect#python" rel="nofollow noreferrer">https://cloud.google.com/alloydb/docs/auth-proxy/connect#python</a> and <a href="https://github.com/GoogleCloudPlatform/alloydb-auth-proxy#example-invocations" rel="nofollow noreferrer">https://github.com/GoogleCloudPlatform/alloydb-auth-proxy#example-invocations</a></p>
  <p>I am using FastAPI in the backend and using sqlalchemy.</p>
  <pre class="lang-py prettyprint-override"><code>
  SQLALCHEMY_DATABASE_URL = &quot;postgresql+psycopg2://&lt;user&gt;:&lt;password&gt;@\
  localhost/postgres&quot;
  
  engine = create_engine(SQLALCHEMY_DATABASE_URL)
  
  SesionLocal = sessionmaker(bind=engine, autocommit=False, autoflush=True)
  </code></pre>
  <p>I start the auth proxy using the credentials</p>
  <pre><code>alloydb-auth-proxy &quot;projects/&lt;project-id&gt;/locations/&lt;region&gt;/clusters/&lt;database-id&gt;/instances/&lt;instance-id&gt;&quot; --credentials-file &quot;key.json&quot;
  </code></pre>
  <p>I am leaving the address and port as default i.e. address to <code>127.0.0.1</code> and port <code>5432</code>.</p>
  <p>The proxy starts</p>
  <pre><code>[projects/&lt;project-id&gt;/locations/&lt;region&gt;/clusters/&lt;database-id&gt;/instances/&lt;instance-id&gt;] Listening on 127.0.0.1:5432
  The proxy has started successfully and is ready for new connections!
  </code></pre>
  <p>But when I run the app it's getting me an error in console-</p>
  <pre><code>sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at &quot;localhost&quot; (::1), port 5432 failed: Connection refused (0x0000274D/10061)
          Is the server running on that host and accepting TCP/IP connections?
  connection to server at &quot;localhost&quot; (127.0.0.1), port 5432 failed: server closed the connection unexpectedly    
          This probably means the server terminated abnormally
          before or while processing the request.
  </code></pre>
  <p>and in proxy cmd</p>
  <pre><code>[projects/&lt;project-id&gt;/locations/&lt;region&gt;/clusters/&lt;database-id&gt;/instances/&lt;instance-id&gt;] failed to connect to instance: Dial error: failed to dial (instance URI = &quot;&lt;project-id&gt;/&lt;region-id&gt;/
  &lt;database-id&gt;/&lt;instance-id&gt;&quot;): dial tcp xx.xx.xx.x:5433: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
  </code></pre>
  <p>What's happening here?</p>