1

I'm trying here to connect a Cloud SQL postgres database from a NestJS server (using prisma as an ORM).

This is the file I'm using to establish the database connection:

schema.prisma:

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

And the DATABASE_URL env variable looks like this:

postgres://postgres:secret@DATABASE_PUBLIC_IP/postgres?socket=/cloudsql/PROJECT_ID:CONNECTION_NAME:production&schema=public&sslmode=prefer

Everything works fine on my local environment, but when I try to deploy it to the cloud it doesn't work...

My Dockerfile:

FROM node:14

RUN apt-get update && apt-get install -y postgresql-client

WORKDIR /usr/src/app

COPY package.json /usr/src/app

RUN npm install

COPY . /usr/src/app
COPY prisma ./prisma/

EXPOSE 3000

RUN npm run prisma:generate
RUN npm run start:dev

The error I get:

enter image description here

Louis Charles
  • 330
  • 4
  • 18
  • What's your Cloud Run Cloud SQL configuration? – guillaume blaquiere Mar 29 '22 at 19:23
  • Not much to say about the Cloud SQL config. I have a public IP address, and granted the compute engine service account (cloud run default service account) Cloud SQL Admin. Everything's on the same region – Louis Charles Mar 29 '22 at 19:33
  • I don't know prisma. I can't say if it set the correct Cloud SQL connection in the Cloud Run configuration. – guillaume blaquiere Mar 29 '22 at 20:01
  • When I'm running the project locally, everything's fine – Louis Charles Mar 29 '22 at 20:03
  • Yes, because locally you haven't a Cloud RUn Cloud SQL connection to set up! – guillaume blaquiere Mar 30 '22 at 07:59
  • I found [this](https://stackoverflow.com/questions/71251937) very similar issue that has the same log that was solved by changing the port. The same with [this one](https://stackoverflow.com/questions/68476229), which was solved by changing the connection time, both related to Prisma. Could you please help us discard Prisma as a root cause? Also, the DATABASE_URL is longer than the 108 supported characters, as described in the [Connection Guide](https://cloud.google.com/sql/docs/postgres/connect-run#connect_to), please try reviewing it, I noticed the SQL port is not being used. – Alex Mar 30 '22 at 15:55

0 Answers0