Prisma Postgres on Deno
Deno Deploy includes a feature that allows you to provision a Prisma Postgres database directly within the platform. This guide demonstrates how to integrate Prisma Postgres in a Deno Deploy project using a minimal Deno application that logs HTTP requests to the database.
By the end of this guide, you will have a deployed Deno app that writes to and reads from a Prisma Postgres database provisioned in Deno Deploy, using Prisma Client with runtime = "deno".
- A Deno Deploy account
- Deno runtime installed (installation guide)
- Deno extension for VS Code
Create a new Deno project using the deno init command, which generates a basic project structure with a main entry file and configuration.
deno init prisma-postgres-deno-deploy
cd prisma-postgres-deno-deployTo ensure VS Code recognizes this as a Deno project and provides proper TypeScript validation, you need to initialize the workspace. Without this, VS Code will show errors when using Deno-specific APIs like Deno.serve.
Install the Deno extension for VS Code, then:
- Select View > Command Palette (or press
Cmd+Shift+Pon macOS orCtrl+Shift+Pon Windows) - Run the command Deno: Initialize Workspace Configuration
Update the main.ts file to create a minimal HTTP server that responds with "Hello, World!". You will add database access to this handler later.
function handler(_req: Request): Response {
return new Response("Hello, World!");
}
Deno.serve(handler);You can test the server locally by running:
deno run devVisit localhost:8000 in your browser to see the application running.
To connect your project to Deno Deploy and get a database connection string, you need to have a successful deployment. Set up a GitHub repository and push your project to it.
Deploy your repository to Deno Deploy. Any subsequent commits will trigger automatic redeployments. You need to deploy now, as the database string requires a successful deployment to generate.
- Navigate to the Deno Deploy dashboard and select New App
- Configure GitHub app permissions by following GitHub's prompts
- Choose your GitHub repository in the Deno Deploy interface
- Click Create App to complete the deployment
The application will deploy automatically.
Provision a Prisma Postgres database in Deno Deploy, and link it to your application:
- Go to the Databases section in the Deno Deploy dashboard
- Select Provision Database and choose Prisma Postgres
- Complete the database configuration and confirm provisioning
- Click Assign and select your application
- Copy the Production connection string
- Add the connection string to your
.envfile:
DATABASE_URL="postgresql://<username>:<password>@db.prisma.io:5432/<database_name>-production"To access the database connection string during local development, configure Deno to load environment variables from your .env file using the --env-file flag.
Update the dev task in deno.json:
{
"tasks": {
"dev": "deno run --watch --env-file main.ts"
}
}Install the Prisma Client, PostgreSQL adapter, and development dependencies:
deno install npm
# couldn't auto-convert command:@prisma/client@7 npm
# couldn't auto-convert command:@prisma/adapter-pg npm
# couldn't auto-convert command:pg npm
# couldn't auto-convert command:prisma@prev npm
# couldn't auto-convert command:@types/pgdeno install npm undefined
# couldn't auto-convert command:@prisma/client@7 npm undefined
# couldn't auto-convert command:@prisma/adapter-pg npm undefined
# couldn't auto-convert command:pg npm undefined
# couldn't auto-convert command:prisma@prev npm undefined
# couldn't auto-convert command:@types/pgdeno install npm undefined
# couldn't auto-convert command:@prisma/client@7 npm undefined
# couldn't auto-convert command:@prisma/adapter-pg npm undefined
# couldn't auto-convert command:pg npm undefined
# couldn't auto-convert command:prisma@prev npm undefined
# couldn't auto-convert command:@types/pgdeno install npm:@prisma/client@7 npm:@prisma/adapter-pg npm:pg npm:prisma@prev npm:@types/pgInitialize Prisma in your project, which creates the necessary configuration files and folder structure for defining your database models.
Initialize Prisma in your project, which creates the necessary configuration files and folder structure for defining your database models.
bunx --bun prisma@prev initpnpm dlx prisma@prev inityarn dlx prisma@prev initnpx prisma@prev initUpdate the Prisma schema with these changes:
- Change the client output from
prisma-client-jstoprisma-client. - Add the Deno runtime configuration. (This is required for Deno to run properly)
- Add the Log model for storing request information.
generator client {
provider = "prisma-client"
output = "../generated/prisma"
runtime = "deno"
}
datasource db {
provider = "postgresql"
}
model Log {
id Int @id @default(autoincrement())
level Level
message String
meta Json
}
enum Level {
Info
Warn
Error
} Migrations create the actual database tables based on your Prisma schema. This command generates SQL migration files and executes them against your database, creating the Log table with the specified fields.
deno run -A npm
# couldn't auto-convert command:prisma@prev migrate dev --name init
deno run -A npm
# couldn't auto-convert command:prisma@prev generatedeno run -A npm undefined
# couldn't auto-convert command:prisma@prev migrate dev --name init
deno run -A npm undefined
# couldn't auto-convert command:prisma@prev generatedeno run -A npm undefined
# couldn't auto-convert command:prisma@prev migrate dev --name init
deno run -A npm undefined
# couldn't auto-convert command:prisma@prev generatedeno run -A npm:prisma@prev migrate dev --name init
deno run -A npm:prisma@prev generateNow that the database is configured, update your application to interact with it. This implementation creates a logging system that captures every HTTP request, stores it in the database, and returns the logged entry as JSON.
import { PrismaClient } from "./generated/prisma/client.ts";
import { PrismaPg } from "npm:@prisma/adapter-pg";
import process from "node:process";
const adapter = new PrismaPg({
connectionString: process.env.DATABASE_URL,
});
const prisma = new PrismaClient({
adapter,
});
async function handler(request: Request) {
const url = new URL(request.url);
if (url.pathname === "/favicon.ico") {
return new Response(null, { status: 204 });
}
const log = await prisma.log.create({
data: {
level: "Info",
message: `${request.method} ${request.url}`,
meta: {
headers: JSON.stringify(request.headers),
},
},
});
const body = JSON.stringify(log, null, 2);
return new Response(body, {
headers: { "content-type": "application/json; charset=utf-8" },
});
}
Deno.serve(handler);Test the application locally by running:
deno run devVisit localhost:8000 in your browser to see the application running. You should see a JSON response containing the log entry:
{
"id": 1,
"level": "Info",
"message": "GET http://localhost:8000/",
"meta": {
"headers": "..."
}
}The build command must generate the Prisma Client code to ensure it is available in production.
- Go to the application in Deno Deploy and click Settings
- Under Build configuration, hit Edit and add
deno run -A npm:prisma@prev generateto the build command - Click Save
Commit and push your changes to trigger an automatic deployment:
git add .
git commit -m "added prisma"
git pushNavigate back to Deno Deploy and you should see a successful build. Once deployed, click the deployment URL at the top right of the dashboard.
When you visit your deployed application, you should see a response that looks like this:
{
"id": 1,
"level": "Info",
"message": "GET https://prisma-postgres-deno-deploy.<org-name>.deno.net/",
"meta": {
"headers": "{}"
}
}Each time you refresh the page, a new log entry is created in your database.
Now that you have a working Deno app connected to a Prisma Postgres database, you can:
- Extend your data model - Add relationships, validations, and indexes to your Prisma schema
- Secure your API - Implement authentication, rate limiting, and proper error handling
- Improve deployment - Set up CI/CD, monitoring, and database backups for production