How To Connect Multiple Database With Node JS and Prisma
Ken Bramasta5 min read
Learn how to connect multiple databases with Node JS and Prisma and create separate data sources for each connection. This step-by-step guide will walk you through the process of setting up multiple Prisma instances in a single project application.

Have you ever been in a situation where you have to access two databases on different platforms in an application?
Well, you found the right place to seek for help! When I researched this, I thought it would be very easy because I thought ORM currently supports the multiple database feature for my needs. But when I did research, I found that Prisma can only support multiple schema in its official documentation.
Given that idea, it couldn't fit in to the business case I was facing, not until I found this
This works like magic! I could access both PostgreSQL and MySQL databases in one application simultaneously.
Now, I'll show you how to do it. Follow me.
Step 1: Prerequisite#
What you need:
- Node JS
- Nest JS CLI
- PostgreSQL
- MySQL
Preparing Repository#
We will be doing this tutorial with a Nest JS template. Therefore, if you haven't installed the NestJS CLI yet, you need to install it globally first
After that, we initialize the project with the command
When there is a choice, we choose npm then after it's done we move to the folder, then we move to the project and install Prisma with the command
Then when it's done, we will initialize Prisma with the command
The above command does 2 things:
- Create a
prismafolder containing theschema.prismafile - Create a
.envfile After all the steps above are done, you're finished preparing the project repository we will use, but you can also check the official documentation if you want to know more.
Preparing Database#
After we've finished setting up the repository, you need to prepare a simulation of the existing databases. In this case, the naming of the 2 databases and each of their tables will be like this:
First Database:
name: first_db
platform: PostgreSQL
table: person

Second Database:
name: second_db
platform: MySQL
table: pets

You can create tables with a structure like the above by running the following queries:
first_db(PostgreSQL)
second_db(MySQL)
Step 2: defining the first database connection#
First, you need to fill in the DATABASE_URL variable in the .env file according to the credentials formed into a connection string like this. In the local env, the value of the env becomes like this
because in this case, we will be using an existing database, we will run
so we can generate schema based on the connected database.
After the above process is successful, we will generate the prisma client with the command.
according to the data model defined in the prisma/schema.prisma file.
Step 3: defining the second database connection#
To define the second part, we need to create a new folder at the root named prisma-second, inside it we will create a file schema.prisma like this:
You might wonder why do we have to add the output to the client. It's because when there are 2 instances of Prisma in one application, we have to specify the output when generating the data model, as mentioned here to avoid overwriting the Prisma client from the first database connection.
Then, we need to add the SECOND_DATABASE_URL variable to the .env file, and you fill in the DB credentials according to the connection string format like this in my case, the value of the variable will be like this:
After that, we will do the DB pull again but with the added schema argument to generate the second connection using this command:
After we finish generating the data model for the second existing database, we will then generate the Prisma client for the second DB using the command:
And we must also add the schema argument to specify the schema we want to generate the Prisma client for. And if you look in the node_modules/@internal there will be a folder called Prisma Second, according to the path we defined in the schema.prisma file in the prisma-second folder.

Step 4: Fetch data from 2 data source#
You will be trying to retrieve data from 2 different data sources. First of all, we will create a folder named prisma under src containing 3 files like this
prisma.service.ts
prisma-second.service.ts
prisma.module.ts
Your folder's structure should be like this when you've added the files.

After defining the Prisma module, we will create an API that will retrieve Person and Pets data from 2 different databases and return the result when we hit the API. To do that, change the files below like this: app.service.ts
app.controller.ts
app.module.ts
Then run the command below to start:
If successful, next you need to open http://localhost:3000 then we will successfully be able to call the API with 2 different data sources

Conclusion#
You can create multiple database connections of different types in one project using Prisma.io as long as you pay attention to the following things:
- Creating separate schema.prisma files that are separate from one another
- Defining output for each Prisma client and ensuring that there are no paths that are the same
- Running the
npx prisma db pullandnpx prisma generatecommands for the other Prisma instances with the--schemaargument to specify which schema we will run - Defining a different Prisma service for each connection.
Resource#
- Full code repository
- Multiple schema documentation
- Multiple Database / Datasource issue
Enjoyed this post?#
Check out some of my other articles:

Tend to this garden with me
Get clean, markdown-first engineering dispatches. No spam, ever.