Connect from Prisma to Neon
Create a Neon project in seconds and connect from Prisma
Prisma is an open-source, next-generation ORM that allows you to easily manage and interact with your database. This guide describes how to connect from Prisma to Neon.
Create a Neon project
If you do not have one already, create a Neon project. Save your connection connection string. It is required when defining connection settings.
- Navigate to the Projects page in the Neon Console.
- Click New Project.
- Specify a name, a PostgreSQL version, a region, and click Create Project.
Connect to Neon from Prisma
To connect to Neon from Prisma:
-
Add the following lines to your
prisma/schema.prisma
file to identify the data source and database URL:datasource db { provider = "postgresql" url = env("DATABASE_URL") }
-
Add a
DATABASE_URL
setting to your.env
file and set it to the Neon connection string that you copied in the previous step.
DATABASE_URL="postgres://<user>:<password>@<host>:5432/neondb"
where:
<host>
the hostname of the Neon compute endpoint. The hostname has anep-
prefix and appears similar to this:ep-tight-salad-272396.us-east-2.aws.neon.tech
.<user>
is the database user.<password>
is the database user's password, which is provided to you when you create a Neon project.
You can find all of the connection details listed above, except for your password, in the Connection Details widget on the Neon Dashboard. For more information, see Connect from any application. If you have misplaced your password, see Reset a password.
Configure a shadow database for Prisma Migrate
Prisma Migrate is a migration tool that allows you to easily evolve your database schema from prototyping to production. Prisma Migrate requires a shadow database to detect schema drift. This section describes how to configure a second Neon database as a shadow database, which is required to run the prisma migrate dev
command.
note
Prisma Migrate requires a direct connection to the database and does not support connection pooling with PgBouncer. Migrations fail with an error if a pooled connection is used. For more information, see Prisma Migrate with PgBouncer.
To configure a shadow database:
-
Create another database in your Neon project and copy the connection string. Refer to Create a database for instructions. For information about obtaining a connection string, see Connect from any application.
-
Add the
shadowDatabaseUrl
setting to yourprisma/schema.prisma
file to identify the shadow database URL:datasource db { provider = "postgresql" url = env("DATABASE_URL") shadowDatabaseUrl = env("SHADOW_DATABASE_URL") }
-
Add a
SHADOW_DATABASE_URL
environment variable to your.env
file and set it to the Neon connection string that you copied in the previous step.SHADOW_DATABASE_URL="postgres://<user>:<password>@<host>:5432/<dbname>"
For more information about shadow databases, refer to About the shadow database, in the Prisma documentation.
Prisma Migrate with PgBouncer
Prisma Migrate requires a direct connection to the database. It does not support connection pooling with PgBouncer. Attempting to run Prisma Migrate commands with a pooled connection causes the following error:
Error undefined: Database error
Error querying the database: db error: ERROR: prepared statement
"s0" already exists
If you encounter this error, ensure that you are using a direct connection to the database. Neon supports both pooled and non-pooled connections to the same database. See Enable connection pooling for more information.
For more information about this issue, refer to the Prisma documentation.
Prisma Client with PgBouncer for serverless functions
Serverless functions may require a large number of database connections. If you are using Prisma Client from a serverless function, enable connection pooling in Neon and add the ?pgbouncer=true
flag to your connection URL. You can enable connection pooling in Neon for individual connections by adding a -pooler
suffix to the endpoint ID, which is part of the hostname. For example:
postgres://sally:<password>@ep-square-sea-260584-pooler.us-east-2.aws.neon.tech/neondb?pgbouncer=true
For more information, see Enable connection pooling. For related information, refer to the Prisma documentation.
Connection timeouts
A connection timeout that occurs when connecting from Prisma to Neon causes an error similar to the following:
Error: P1001: Can't reach database server at `ep-white-thunder-826300.us-east-2.aws.neon.tech`:`5432`
Please make sure your database server is running at `ep-white-thunder-826300.us-east-2.aws.neon.tech`:`5432`.
This error most likely means that the Prisma query engine timed out before the Neon compute instance was activated.
A Neon compute instance has two main states: Active and Idle. Active means that the compute instance is currently running. If there is no query activity for 5 minutes, Neon places a compute instance into an idle state. For more information, see Compute lifecycle.
When you connect to an idle compute instance from Prisma, Neon automatically activates it. Activation typically happens within a few seconds, but added latency can result in a connection timeout. To address this issue, try adjusting your Neon connection string with the following parameters:
-
Set
connect_timeout
to 0 or a higher value. This setting defines the maximum number of seconds to wait for a new connection to be opened. The default value is 5 seconds. A setting of 0 means no timeout. A higher setting should provide the time required to avoid connection timeout issues. For example:postgres://sally:<password>@ep-square-sea-260584-pooler.us-east-2.aws.neon.tech/neondb?connect_timeout=10
-
If you are using connection pooling, set
pool_timeout
to 0 or a higher value. This setting defines the number of seconds to wait for a new connection from the pool. The default is 10 seconds. A setting of 0 means no timeout. A higher setting should provide the time required to avoid connection timeout issues. For example:postgres://sally:<password>@ep-square-sea-260584-pooler.us-east-2.aws.neon.tech/neondb?pgbouncer=true&pool_timeout=20
For additional information about connecting from Prisma, refer to the following resources in the Prisma documentation:
- Connection management
- Database connection issues
- PostgreSQL database connector
- Increasing the pool timeout
Need help?
Send a request to support@neon.tech, or join the Neon community forum.