This guide shows you how to automatically create and delete Prisma Postgres databases using GitHub Actions and the Prisma Postgres REST API. The setup provisions a new database for every pull request, seeds it with sample data, and the github-actions bot leaves a comment with the database name and the status.
After the PR is closed, the database is automatically deleted. This allows you to test changes in isolation without affecting the main development database.
In this section, you'll set up Prisma in your project and verify that it works locally before integrating it into GitHub Actions. This involves installing Prisma's dependencies, connecting to a Prisma Postgres database, defining your data models, applying your schema, and seeding the database with sample data.
By the end of this section, your project will be fully prepared to use Prisma both locally and in a CI workflow.
Create a Prisma Postgres database and replace the generated DATABASE_URL in your .env file with the postgres://... connection string from the CLI output:
generator client {
provider = "prisma-client"
output = "../src/generated/prisma"
}
datasource db {
provider = "postgresql"
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
authorId Int
author User @relation(fields: [authorId], references: [id])
}
Create a prisma.config.ts file to configure Prisma with seeding:
{"name": "prisma-gha-demo","version": "1.0.0","description": "","scripts": {"seed": "tsx src/seed.ts" },// other configurations...}
Then run:
bun
bun run seed
bunx prisma studio
pnpm
pnpm run seed
pnpm prisma studio
yarn
yarn seed
yarn prisma studio
npm
npm run seed
npx prisma studio
Navigate to http://localhost:5555 and verify that the database has been seeded with sample data. Now you're ready to automate this process with GitHub Actions.
In this step, you will set up a GitHub Actions workflow that automatically provisions a Prisma Postgres database when a new pull request (PR) is opened. Once the PR is closed, the workflow will clean up the database.
Paste the following into .github/workflows/prisma-postgres-rest-api.yml. This sets up when the workflow runs and provides required environment variables.
.github/workflows/prisma-postgres-rest-api.yml
name: Prisma Postgres REST API Workflow
on:
pull_request:
types: [opened, reopened, closed]
workflow_dispatch:
inputs:
action:
description: "Action to perform"
required: true
default: "provision"
type: choice
options:
- provision
- cleanup
database_name:
description: "Database name (for testing, will be sanitized)"
required: false
type: string
env:
PRISMA_POSTGRES_SERVICE_TOKEN: ${{ secrets.PRISMA_POSTGRES_SERVICE_TOKEN }}
PRISMA_PROJECT_ID: ${{ secrets.PRISMA_PROJECT_ID }}
# Sanitize database name once at workflow level
DB_NAME: ${{ github.event.pull_request.number != null && format('pr-{0}-{1}', github.event.pull_request.number, github.event.pull_request.head.ref) || (inputs.database_name != '' && inputs.database_name || format('test-{0}', github.run_number)) }}
# Prevent concurrent runs of the same PR
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
Now you will be adding the provision and cleanup jobs to this workflow. These jobs will handle the creation and deletion of Prisma Postgres databases based on the pull request events.
When a pull request is closed, you can automatically remove the associated database by adding a cleanup job. The cleanup job:
Finds the database by name
Deletes it from the Prisma Postgres project
Can also be triggered manually with action: cleanup
Append the following to your jobs: section, after the provision-database job:
.github/workflows/prisma-postgres-rest-api.yml
cleanup-database:
if: (github.event_name == 'pull_request' && github.event.action == 'closed') || (github.event_name == 'workflow_dispatch' && inputs.action == 'cleanup')
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Validate Environment Variables
run: |
if [ -z "${{ env.PRISMA_POSTGRES_SERVICE_TOKEN }}" ]; then
echo "Error: PRISMA_POSTGRES_SERVICE_TOKEN secret is not set"
exit 1
fi
if [ -z "${{ env.PRISMA_PROJECT_ID }}" ]; then
echo "Error: PRISMA_PROJECT_ID secret is not set"
exit 1
fi
- name: Sanitize Database Name
run: |
# Sanitize the database name
DB_NAME="$(echo "${{ env.DB_NAME }}" | tr '/' '_' | tr '-' '_' | tr '[:upper:]' '[:lower:]')"
echo "DB_NAME=$DB_NAME" >> $GITHUB_ENV
- name: Delete Database
run: |
echo "Fetching all databases..."
RESPONSE=$(curl -s -X GET \
-H "Authorization: Bearer ${{ env.PRISMA_POSTGRES_SERVICE_TOKEN }}" \
-H "Content-Type: application/json" \
"https://api.prisma.io/v1/projects/${{ env.PRISMA_PROJECT_ID }}/databases")
echo "Looking for database with name: ${{ env.DB_NAME }}"# Extract database ID using jq to properly parse JSON
DB_EXISTS=$(echo "$RESPONSE" | jq -r ".data[]? | select(.name == \"${{ env.DB_NAME }}\") | .id")
if [ ! -z "$DB_EXISTS" ] && [ "$DB_EXISTS" != "null" ]; then
echo "Database ${{ env.DB_NAME }} exists with ID: $DB_EXISTS. Deleting..."
DELETE_RESPONSE=$(curl -s -X DELETE \
-H "Authorization: Bearer ${{ env.PRISMA_POSTGRES_SERVICE_TOKEN }}" \
-H "Content-Type: application/json" \
"https://api.prisma.io/v1/databases/$DB_EXISTS")
echo "Delete API Response: $DELETE_RESPONSE"
if echo "$DELETE_RESPONSE" | grep -q '"error":'; then
ERROR_MSG=$(echo "$DELETE_RESPONSE" | jq -r '.message // "Unknown error"')
echo "Failed to delete database: $ERROR_MSG"
exit 1
else
echo "Database deletion initiated successfully"
fi
else
echo "No existing database found with name ${{ env.DB_NAME }}"
fi
This completes your Prisma Postgres management workflow setup. In the next step, you'll configure the required GitHub secrets to authenticate with the Prisma API.
To manage Prisma Postgres databases, you also need a service token. Follow these steps to retrieve it:
Make sure you are in the same workspace where you created your project in the last step.
Navigate to the Settings page of your workspace and select Service Tokens.
Click New Service Token.
Copy the generated token and save it in your .env file as PRISMA_POSTGRES_SERVICE_TOKEN. This token is required for the next step's script and must also be added to your GitHub Actions secrets.
5.2 Retrieve the project ID where you want to provision Prisma Postgres databases
To avoid conflicts with your development databases, you'll now create a dedicated project specifically for CI workflows. Use the following curl command to create a new Prisma Postgres project using the Prisma Postgres REST API:
Replace the $PRISMA_POSTGRES_SERVICE_TOKEN with the service token and the $PROJECT_NAMEwith a name for your project (e.g.,my-gha-preview). The script will create a new Prisma Postgres project in the us-east-1 region.
You now have a fully automated GitHub Actions setup for managing ephemeral Prisma Postgres databases.
This gives you:
Isolated databases for every pull request.
Automatic schema sync and seed.
Cleanup of unused databases after merges.
Because each pull request gets its own database, changes can be tested without touching a shared development database. You can extend this by running your test suite against the provisioned database.