I've been trying to connect to my Cloud SQL instance using the pg
module but haven't been successful so far.
I've looked around a lot online but couldn't understand much on the topic. I also would like to deploy my Express app on Cloud Run at some point and have it connect to my Cloud SQL instance but I don't know how to go about doing that.
Here's a list of things I don't understand and would like a brief explanation on:
- What are Unix socket connections and why should I use them over normal connections?
- What is a Cloud SQL Proxy? Do I need to use it? If so, why?
- Would I need to do any extra work to connect to my Cloud SQL instance from Cloud Run?
Here are all the connection objects and connection strings I have tried with the pg.Client
object:
- First connection string:
postgresql+psycopg2://postgres:password@/cloudsql/myapp:us-central1:mydb?host=/var/lib/postgresql
- Second connection string:
postgresql://postgres:password@hostip:5432/myapp:us-central1:mydb
- Third connection string:
postgresql://postgres:password@hostip:5432/sarcdb
- Connection object:
{ host: "/cloudsql/myapp:us-central1:mydb", username: "postgres", password: "password", database: "mydb" }
All of these give me a Connection terminated unexpectedly
error.