Prisma Postgres provides a serverless PostgreSQL database with a generous free tier. It offers instant provisioning, automatic scaling, and built-in connection pooling, perfect for serverless environments like Val Town.
1. Create an instant database
Section titled “1. Create an instant database”The fastest way to get a Prisma Postgres database is via the CLI. Run this command in your terminal:
npx create-db --jsonYou’ll get a response like this:
{ "connectionString": "postgresql://...@db.prisma.io:5432/postgres?sslmode=require", "claimUrl": "https://create-db.prisma.io/claim?projectID=...", "deletionDate": "2025-11-27T11:03:38.153Z", "region": "ap-southeast-1", "name": "2025-11-26T11:03:34.105Z", "projectId": "proj_..."}Copy the connectionString value. You’ll need this to connect from Val Town.
2. Save your connection string
Section titled “2. Save your connection string”Go to val.town and click Env Variables in the left sidebar.
Click New env variable, then set the key to PRISMA_POSTGRES_URL and paste
your connection string as the value.
3. Create your first table
Section titled “3. Create your first table”Copy and paste this val to create a table:
import { Client } from "jsr:@db/postgres";
const client = new Client(Deno.env.get("PRISMA_POSTGRES_URL"));await client.connect();
await client.queryObject` CREATE TABLE IF NOT EXISTS users ( id SERIAL PRIMARY KEY, name TEXT NOT NULL, email TEXT NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP )`;
console.log("Table created!");await client.end();4. Insert some data
Section titled “4. Insert some data”Use template strings to safely insert data (this prevents SQL injection):
import { Client } from "jsr:@db/postgres";
const client = new Client(Deno.env.get("PRISMA_POSTGRES_URL"));await client.connect();
await client.queryObject` INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com')`;
await client.queryObject` INSERT INTO users (name, email) VALUES ('Bob', 'bob@example.com')`;
console.log("Data inserted!");await client.end();5. Query your data
Section titled “5. Query your data”Get back the data you just inserted:
import { Client } from "jsr:@db/postgres";
const client = new Client(Deno.env.get("PRISMA_POSTGRES_URL"));await client.connect();
const result = await client.queryObject` SELECT id, name, email, created_at FROM users ORDER BY id`;
console.log(result.rows);await client.end();6. Query with parameters
Section titled “6. Query with parameters”For dynamic queries, use prepared statements to prevent SQL injection:
import { Client } from "jsr:@db/postgres";
const client = new Client(Deno.env.get("PRISMA_POSTGRES_URL"));await client.connect();
const name = "Alice";const result = await client.queryObject` SELECT id, name, email FROM users WHERE name = ${name}`;
console.log(result.rows);await client.end();