0

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

joana
  • 23
  • 3
  • check suggestions in this stackoverflow [link1](https://stackoverflow.com/questions/72118668/cloud-functions-prismaclientinitializationerror-cant-reach-database-server-a), [link2](https://stackoverflow.com/a/72500709/18265638),[link3](https://stackoverflow.com/a/68495296/18265638) – Sathi Aiswarya Oct 24 '22 at 10:52
  • Link1 and link2 are for MySQL, tried it but doesn't work. Link3 doesn't work either – joana Oct 24 '22 at 11:12
  • can you check the format of the connection string as mentioned in this [github](https://github.com/prisma/prisma/issues/14013#issuecomment-1169146188) – Sathi Aiswarya Oct 24 '22 at 11:33
  • Can't reach database server at `localhost`:`5432`. Doesn't work either – joana Oct 24 '22 at 13:54

2 Answers2

0

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.

kurtisvg
  • 3,412
  • 1
  • 8
  • 24
0

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.

joana
  • 23
  • 3