Prisma Postgres

Verified 中级 Intermediate 参考型 Reference ⚡ Claude Code 专属 ⚡ Claude Code Optimized
3 min read · 174 lines

Prisma official managed Postgres: Console + CLI + Management API + SDK workflows

Prisma Postgres

Overview

Guide for creating, managing, and integrating Prisma Postgres -- a managed serverless PostgreSQL service. Covers Console workflows, instant CLI provisioning with create-db, the Management API for programmatic control, and the typed SDK.

Core Workflows

1. Console-First

  • Open https://console.prisma.io
  • Create/select workspace and project
  • Use Studio to view/edit data
  • Retrieve connection details from project UI

2. Quick Provisioning with create-db

Fastest way to get a working database:

npx create-db@latest

Aliases: npx create-pg@latest / npx create-postgres@latest

Options

Flag Short Description
--region [string] -r Region: ap-southeast-1, ap-northeast-1, eu-central-1, eu-west-3, us-east-1, us-west-1
--interactive -i Open region selector
--json -j Machine-readable JSON output
--env [string] -e Write DATABASE_URL and CLAIM_URL to target .env

Lifecycle

  • Databases are temporary by default
  • Unclaimed databases auto-delete after ~24 hours
  • Claim via the URL in command output to keep permanently

Common Patterns

# Quick database
npx create-db@latest

# Region-specific
npx create-db@latest --region eu-central-1

# Interactive region selection
npx create-db@latest --interactive

# Write env vars for app bootstrap
npx create-db@latest --env .env

# CI-friendly output
npx create-db@latest --json

Programmatic Usage (Library API)

npm install create-db
import { create, isDatabaseSuccess, isDatabaseError } from "create-db";

const result = await create({
  region: "us-east-1",
  userAgent: "my-app/1.0.0",
});

if (isDatabaseSuccess(result)) {
  console.log(result.connectionString);
  console.log(result.claimUrl);
  console.log(result.deletionDate);
}
import { regions } from "create-db";
const available = await regions();

3. Management API

Base URL: https://api.prisma.io/v1

Explore: OpenAPI docs | Swagger Editor

Authentication

Method Use Case
Service Token Server-to-server in your own workspace
OAuth 2.0 Acting on behalf of users across workspaces

Service Token

Create in Console workspace settings, send as Bearer auth:

Authorization: Bearer $TOKEN

OAuth Flow

  1. Redirect to https://auth.prisma.io/authorize with client_id, redirect_uri, response_type=code, scopes
  2. Receive code on callback
  3. Exchange at https://auth.prisma.io/token
  4. Use access token in API requests

Common Endpoints

  • GET /workspaces
  • GET /projects
  • POST /projects
  • Database management under project/database paths

4. Management API SDK

npm install @prisma/management-api-sdk
  • createManagementApiClient - for existing tokens
  • createManagementApiSdk - for OAuth + token refresh

Connection Setup

import { PrismaClient } from '../generated/client'
import { PrismaPostgresAdapter } from '@prisma/adapter-ppg'

const prisma = new PrismaClient({
  adapter: new PrismaPostgresAdapter({
    connectionString: process.env.PRISMA_DIRECT_TCP_URL,
  }),
})

Local Development

Use prisma dev for a local Prisma Postgres instance:

prisma dev              # Start local database
prisma dev --detach     # Background mode
prisma dev ls           # List instances
prisma dev stop myproj  # Stop instance

When ready for production, switch to cloud:

prisma init --db

Update DATABASE_URL to the cloud connection string.

Resources

相关技能 Related Skills