Database
In this section, we'll discuss what happens with the database when your app goes live. When you develop your app locally, you probably use a local development database that Wasp starts when needed. However, when it's time to deploy your app, you'll need to set up a production database.
Production database requirements
The server app that Wasp generates uses a PostgreSQL database. The only requirement from Wasp's point of view is that the database is accessible from the server via the DATABASE_URL server env variable.
It can be a PostgreSQL database running on the same server as the server app, or it can be a managed PostgreSQL database service like Fly Postgres, AWS RDS, Prisma Postgres, or some other service. PostgreSQL can behave differently across major versions, so development and production work best when they use the same major version.
If your provider gives you a pooled and a direct connection string, read Connection poolers to choose which one to use.
Migrations
Every time you make a change in your Prisma schema e.g. adding a new model, changing a field type, etc., you need to create a migration. Migrations are some code that describes the change you made in the schema, and they are used to apply the change to the database.
The benefit of migrations is that you can apply the same change to multiple databases. If there are multiple people working on the project, they can all apply the same changes to their local databases. When you deploy the app to production, the same chaanges are applied to the production database.
Creating migrations
After you made a change in the Prisma schema, you can create a migration by running the following command:
wasp db migrate-dev
This command will create a new migration in the migrations directory. The migration is a set of SQL commands that describe the change you made in the schema.
Applying migrations
In development, the migrations are applied as soon as you run the wasp start command.
In production, the server app first checks if there are any new migrations that need to be applied, and if there are, it applies them before starting the server. This way, the database schema is always in sync with the Prisma schema.
In the built server app, there are two npm scripts: start and start-production. The start script is used in development, and the start-production script is used in production. The start-production script first applies any pending migrations before starting the server.
The migrations might fail to apply if there is a conflict with the existing data in the database. In that case, you'll need to fix the migration and try again.
Debugging failed migrations
If a migration fails to apply, the server app will log the error message and stop. You should then connect to the production database and see what went wrong. If you check the _prisma_migrations table, you'll see the failed migration there.
You can try resolving the error e.g. if you tried adding a @unique constraint to a field that already has duplicate values:
- Remove any duplicate values from the database
- Remove the failed migration from the
_prisma_migrationstable - Try applying the migration again by restarting the server app
_prisma_migrations tableConnection poolers
Many managed PostgreSQL services (for example Supabase, Neon, or Prisma Postgres) give you more than one connection string:
-
A pooled connection string. Might also be called
DATABASE_URLor a transaction-mode pooled connection string.This connection goes through a connection pooler like PgBouncer. The pooler lets many server instances share a few database connections, which matters most when your server runs on a serverless platform or as many instances. However, it can't be used to run migrations.
-
A direct connection string. Might also be called
DIRECT_URL. Session-mode pooled connection strings can also be used as the direct connection string.This connection connects straight to the database, bypassing any pooler. It is suitable for running migrations directly, but might overload your database if you have many concurrent connections.
Wasp has a single env variable for the database connection, DATABASE_URL, and uses it for both your runtime queries and running your migrations when the server starts.
Migrations need a direct or session-mode pooled connection; they can't run through a transaction-mode pooler. Wasp doesn't yet have a separate setting for the URL that migrations should use. We are tracking this in #5582.
Until then, use the direct connection string for everything: set DATABASE_URL to it and ignore the pooled one. This works with every deployment method, including Wasp Deploy and the generated Dockerfile.
The trade-off is that the server's queries bypass the pooler too. Each server instance keeps its own pool of open connections, and managed services cap the number of direct connections. Check your provider's limit before you run several server instances. If your server does need the pooler (for example, it runs on a serverless platform, or you run more instances than your provider allows direct connections for), follow the Connection poolers guide. It shows how to send the server's queries through the pooler while migrations keep using the direct connection.
Use fewer connections
Prisma can be configured to use fewer connections by setting the connection_limit parameter in the connection string. For example, you can set it to 5 like this:
DATABASE_URL="postgresql://user:password@host:port/dbname?connection_limit=5"
Connect to the production database
In development, you can use the wasp db studio command to open a web-based database management tool that allows you to inspect the database.
You can use the same tool to inspect the production database, but you'll need to set the DATABASE_URL env variable to point to the production database. Set the DATABASE_URL env variable in your terminal before running the wasp db studio command:
DATABASE_URL="postgresql://user:password@host:port/dbname" wasp db studio
DATABASE_URL env variableSetting the DATABASE_URL env variable in the .env.server file to point to your production database also works, but then you might forget to remove it and you could accidentally make changes to the production database when you run wasp start in development.
That's why we recommend setting the DATABASE_URL env variable in the terminal to avoid this.
If you are looking how to connect to a Fly.io production database, we wrote a guide on how to do that:
Database Studio with Fly.io »
Connect to your Fly.io production database and run wasp db studio
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 DATABASE_URL. It goes through the pooler and supports IPv4, but still supports migrations, unlike the transaction-mode one.