|
| 1 | +--- |
| 2 | +title: 'Fly.io' |
| 3 | +metaTitle: 'Deploy Prisma Postgres apps to Fly.io' |
| 4 | +metaDescription: 'Learn how to deploy applications using Prisma Postgres to Fly.io.' |
| 5 | +tocDepth: 3 |
| 6 | +toc: true |
| 7 | +--- |
| 8 | + |
| 9 | +[Fly.io](https://fly.io/) is a cloud application platform that lets you deploy full-stack applications globally. This guide shows you how to deploy a Node.js application using Prisma Postgres to Fly.io. |
| 10 | + |
| 11 | +## Prerequisites |
| 12 | + |
| 13 | +- A [Fly.io account](https://fly.io/docs/getting-started/launch/) |
| 14 | +- The [Fly CLI](https://fly.io/docs/flyctl/install/) installed |
| 15 | +- An existing application using [Prisma Postgres](/postgres) (see [Quickstart](/getting-started/prisma-postgres/quickstart/prisma-orm)) |
| 16 | + |
| 17 | +## Deploy your application |
| 18 | + |
| 19 | +### 1. Generate Prisma Client in `postinstall` |
| 20 | + |
| 21 | +Ensure your `package.json` includes a `postinstall` script to generate Prisma Client during deployment: |
| 22 | + |
| 23 | +```json file=package.json |
| 24 | +{ |
| 25 | + // ... |
| 26 | + "scripts": { |
| 27 | + // ... |
| 28 | + // add-next-line |
| 29 | + "postinstall": "prisma generate" |
| 30 | + } |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +### 2. Launch your app with Fly.io |
| 35 | + |
| 36 | +From your project directory, run: |
| 37 | + |
| 38 | +```terminal |
| 39 | +fly launch |
| 40 | +``` |
| 41 | + |
| 42 | +Follow the prompts to configure your application. Fly.io will auto-detect your Node.js application and create the necessary configuration. |
| 43 | + |
| 44 | +### 3. Set your database connection string |
| 45 | + |
| 46 | +Add your Prisma Postgres `DATABASE_URL` as a secret in Fly.io: |
| 47 | + |
| 48 | +```terminal |
| 49 | +fly secrets set DATABASE_URL="your-prisma-postgres-connection-string" |
| 50 | +``` |
| 51 | + |
| 52 | +:::tip |
| 53 | +You can find your `DATABASE_URL` in your `.env` file or in the [Prisma Console](https://console.prisma.io). |
| 54 | +::: |
| 55 | + |
| 56 | +### 4. Deploy |
| 57 | + |
| 58 | +If not already deployed during `fly launch`, deploy your application: |
| 59 | + |
| 60 | +```terminal |
| 61 | +fly deploy |
| 62 | +``` |
| 63 | + |
| 64 | +Once the deployment completes, you'll see a success message with your app's URL: |
| 65 | + |
| 66 | +``` |
| 67 | +🎉 SUCCESS! Your app is live and ready to use! 🎉 |
| 68 | +
|
| 69 | +Visit: https://your-app-name.fly.dev/ |
| 70 | +``` |
| 71 | + |
| 72 | +:::note |
| 73 | +In the Fly.io dashboard, your app's status may show as "Pending" initially. It typically transitions to "Deployed" within a few minutes. |
| 74 | +::: |
| 75 | + |
| 76 | +## Additional considerations |
| 77 | + |
| 78 | +### Ensure your project uses the correct environment variable |
| 79 | + |
| 80 | +Ensure that the data source in your `prisma.config.ts` file is configured to use the `DATABASE_URL` environment variable: |
| 81 | + |
| 82 | +```ts file=prisma.config.ts |
| 83 | +import 'dotenv/config'; |
| 84 | +import { defineConfig, env } from 'prisma/config'; |
| 85 | + |
| 86 | +export default defineConfig({ |
| 87 | + datasource: { |
| 88 | + url: env('DATABASE_URL'), |
| 89 | + }, |
| 90 | + schema: './prisma/schema.prisma', |
| 91 | +}); |
| 92 | +``` |
| 93 | + |
| 94 | +### Running migrations in production |
| 95 | + |
| 96 | +To run migrations on your deployed Fly.io app, you can use: |
| 97 | + |
| 98 | +```terminal |
| 99 | +fly ssh console -C "npx prisma migrate deploy" |
| 100 | +``` |
| 101 | + |
| 102 | +Or add a release command to your `fly.toml`: |
| 103 | + |
| 104 | +```toml file=fly.toml |
| 105 | +[deploy] |
| 106 | + release_command = "npx prisma migrate deploy" |
| 107 | +``` |
| 108 | + |
| 109 | +### Scaling and regions |
| 110 | + |
| 111 | +Fly.io lets you scale and place your application in [multiple regions](https://fly.io/docs/reference/regions/). For optimal performance, deploy your app in a region close to your Prisma Postgres database region. |
| 112 | + |
| 113 | +```terminal |
| 114 | +fly scale count 2 --region iad |
| 115 | +``` |
| 116 | + |
| 117 | +## Troubleshooting |
| 118 | + |
| 119 | +### Prisma schema not found or Client not generated correctly |
| 120 | + |
| 121 | +If you're using a custom Dockerfile and your build fails because the Prisma Client cannot be found, it's likely due to one of two reasons: |
| 122 | + |
| 123 | +1. **Order of operations**: `npm install` (which runs `postinstall`) runs before the schema is copied. |
| 124 | +2. **Incorrect copy path**: The schema is copied to the root instead of the `prisma` folder. |
| 125 | + |
| 126 | +**Solution:** In your Dockerfile, copy the `prisma/` directory to `./prisma` **before** running `npm install`: |
| 127 | + |
| 128 | +```dockerfile |
| 129 | +# ❌ Wrong order or path |
| 130 | +COPY package*.json ./ |
| 131 | +COPY prisma . # Copies contents to root (wrong structure) |
| 132 | +RUN npm install # postinstall runs but might fail or generate in wrong place |
| 133 | + |
| 134 | +# ✅ Correct order and path |
| 135 | +COPY package*.json ./ |
| 136 | +COPY prisma ./prisma # Copies to ./prisma folder (preserves structure) |
| 137 | +RUN npm install # postinstall finds schema in expected location |
| 138 | +COPY . . |
| 139 | +``` |
| 140 | + |
| 141 | +## More information |
| 142 | + |
| 143 | +- [Fly.io Node.js documentation](https://fly.io/docs/languages-and-frameworks/node/) |
| 144 | +- [Fly.io Prisma documentation](https://fly.io/docs/js/prisma/) |
| 145 | +- [Prisma Postgres documentation](/postgres) |
| 146 | + |
0 commit comments