-
QuestionI'm using Normally the node 1 is the primary, and the other two are Having this configuration: generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}I get the error: This is because my Thanks in advance! How to reproduce (optional)Expected behavior (optional)No response Information about Prisma Schema, Client Queries and Environment (optional)// Add your schema.prisma
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
shadowDatabaseUrl = env("DATABASE_URL_N2")
}
/**...*/
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Hi @Aw3same 👋 Thank you for raising this question. Prisma Client currently does not have built-in support for managing read and write replicas in a PostgreSQL cluster. In your current configuration, Prisma is using the One possible workaround could be to manage the database connections at the application level. You could create two instances of PrismaClient, one for read operations and one for write operations, each with its own connection URL. Here's an example: import { PrismaClient } from '@prisma/client'
const readClient = new PrismaClient()
const writeClient = new PrismaClient({ datasources: { mydb: { url: process.env.WRITE_DB_URL }} })In this example, readClient would connect to the read-only replica and writeClient would connect to the primary node. You would need to manage the WRITE_DB_URL environment variable to always point to the primary node. It might interest you to note that there is an extension for working with read replicas here. However, the core functionality of determining which node is writable will have to be handled by you since we do not have support for it. If this answers your question, it would be great if you could mark this Discussion as answered to indicate that it has been resolved. Otherwise please let us know how else we can help you further or close the Discussion if it was resolved in some other way 🙏 |
Beta Was this translation helpful? Give feedback.
Hi @Aw3same 👋
Thank you for raising this question.
Prisma Client currently does not have built-in support for managing read and write replicas in a PostgreSQL cluster. In your current configuration, Prisma is using the
DATABASE_URLenvironment variable to connect to the database. If this URL points to a read-only replica, you will not be able to perform write operations.One possible workaround could be to manage the database connections at the application level. You could create two instances of PrismaClient, one for read operations and one for write operations, each with its own connection URL. Here's an example: