Connection poolers
Last checked with Wasp 0.26.
This guide depends on external libraries or services, so it may become outdated over time. We do our best to keep it up to date, but make sure to check their documentation for any changes.This guide shows you how to send your Wasp server's database queries through a connection pooler, while migrations keep using a direct connection to the database.
Wasp doesn't have built-in support for a separate migration URL yet, so this setup needs a few manual steps. Before you follow it, read Connection poolers in the database docs: for most apps, using the direct connection string for everything is simpler and works just as well.
When you need this
Use a pooled connection for the server's queries when:
- Your server runs on a serverless platform, where each request can start a new server instance with its own connections.
- You run more server instances than your database provider allows direct connections for.
In this setup, your Wasp server will use the pooled connection string for all its queries and actions, while migrations will use the direct connection string. This allows you to benefit from connection pooling without affecting the migration process.
Importantly, nothing changes in development: while using wasp start, you still use the single DATABASE_URL for both the server and migrations. The separation only applies in production.
Provider setup
1. Check your connection strings
Go into your database provider's dashboard and locate the connection strings for your database. You should find:
- A pooled connection string. Might also be called
DATABASE_URLor a transaction-mode pooled connection string. - A direct connection string. Might also be called
DIRECT_URL. Session-mode pooled connection strings can also be used as the direct connection string.
If your provider has specific instructions for Prisma, make sure to follow those and use their recommended connection strings.
directUrl to your Prisma schemaMany providers' Prisma guides also tell you to add directUrl = env("DIRECT_URL") to the datasource block in schema.prisma. Skip that step. Wasp doesn't set DIRECT_URL in development, so wasp start and the wasp db commands would fail.
2. Set the environment variables in production
Go to your production environment's settings or dashboard where you can set environment variables, and add the following:
| Name | Value | Description |
|---|---|---|
DATABASE_URL | Pooled connection string | Will be used for all the queries and actions in your app. |
DIRECT_URL | Direct connection string | Will be used for applying migrations. |
Server setup
In production, Wasp applies pending migrations with its internal db-migrate-prod script. That script uses DATABASE_URL, so you need to run it with DATABASE_URL set to the direct connection string. How you do that depends on how you deploy your server:
With a generated Dockerfile
Most deployment methods, including Wasp Deploy and our self-hosted guides, use the Dockerfile that Wasp generates. Its server applies migrations every time it starts, using its own DATABASE_URL.
To change that, create a Dockerfile in your project's root directory with the following content. Wasp appends it to the end of the generated Dockerfile, so it becomes part of the server's final build stage:
ENTRYPOINT ["sh", "-c", "DATABASE_URL=$DIRECT_URL npm run db-migrate-prod && exec npm run start"]
This replaces the command that starts the server. It overrides the DATABASE_URL for the migrations, and then starts the server with the regular URL. If DIRECT_URL isn't set, the migrations will fail and the server won't start.
The generated Dockerfile can change in future Wasp versions. After you upgrade Wasp, run wasp dockerfile to check that your ENTRYPOINT still matches the generated one, apart from the DATABASE_URL override.
With a build or release command you control
Some platforms let you run your own command when you deploy, for example a build or release step. Run the migrations there, with the direct connection string:
cd .wasp/out/server && DATABASE_URL=$DIRECT_URL npm run db-migrate-prod
Then, if your platform has a start command, change it too. The usual one, npm run start-production, will try to apply migrations again. Instead, you should use this, which only starts the server:
cd .wasp/out/server && NODE_ENV=production npm run start
Our Vercel guide uses this approach, with POSTGRES_URL_NON_POOLING (Supabase's session-mode pooler, which Vercel's Supabase integration provides) as the connection string for migrations.
Troubleshooting
Supabase IPv4/IPv6 considerations
Supabase's direct connections only work over IPv6 by default. At the same time, in your server environment, some hosting providers and self-hosting options may not support IPv6.
If your server's host can't connect over IPv6, look for your database's session-mode pooled connection string, and use that as your DIRECT_URL. It goes through the pooler and supports IPv4, but still supports migrations, unlike the transaction-mode one.