Regarding the setting of the url of the datasource db in the schema.prisma file

Viewed 34

I want to set the URL in the schema.prisma setting by concatenating the strings as follows, but an error occurs.

My desire is to set it without adding the DATABASE_URL environment variable.

Is such a configuration possible? I would be happy if I could simply use schema.prisma to concatenate the strings, but...

datasource db {
  provider = "postgresql"
  url      = "postgresql://" + env("PG_USER") + ":" + env("PG_PASS") + "@" + env("PG_HOST") + ":" + env("PG_PORT") + "/" + env("PG_DATABASE") + "?schema=public"
}
1 Answers

schema.prisma file does not understand how to concatenate the environment variables as it is not a javascript or typescript file. Prisma expects that you specify your environment variable in .env file

// .env file
DATABASE_URL_WITH_SCHEMA=${DATABASE_URL}?schema=foo

and in your schema.prisma file, you load it as shown

// schema.prisma
datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

To learn more about using environment variables with Prisma, check out the docs

Related