Home > Software design >  Can't connect to Postgres CloudSQL from Prisma in Cloudfunction
Can't connect to Postgres CloudSQL from Prisma in Cloudfunction

Time:10-25

I'm trying to connect to a public IP CloudSQL Postgres database from a cloud function with Prisma. The database has SSL enforced. I assume I can use the Cloud Auth Proxy, and it works locally, but when I deploy it gives me an error.

I've tried both:

Option 1:

datasource db {
  provider = "postgresql"
  url = "postgresql://USER:PASSWORD@localhost:3307/DATABASE_NAME?host=CONNECTION_URL"
}

Got error:

Can't reach database server at `CONNECTION_URL`:`3307`

Option 2:

datasource db {
      provider = "postgresql"
      url = "postgresql://USER:PASSWORD@localhost/DATABASE_NAME?host=CONNECTION_URL"
    }

Got error:

Can't reach database server at `IP_ADDRESS`:`5432`

Where IP_ADDRESS is the correct public IP address for the database that I can see in the console

CONNECTION_URL is /cloudsql/PROJ:REGION:INSTANCE

CodePudding user response:

You application is trying to connect on "CONNECTION_URL" and "3307". To use public IP on Cloud Functions you should by connecting by unix socket: https://cloud.google.com/sql/docs/mysql/connect-functions#connect_to

Based on this issue, it looks like your URL should be something like:

postgresql://username@localhost/databasename?host=/cloudsql/CONNECTION_NAME

Where your instance connection name is PROJ:REGION:INSTANCE.

CodePudding user response:

Figure out I had the vpc connector enabled to all traffic, so all traffic was trying to go through the private VPC instead of the public IP. Disabling it fixed it.

  • Related