Skip to content

Prisma Postgres

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.

The fastest way to get a Prisma Postgres database is via the CLI. Run this command in your terminal:

Terminal window
npx create-db --json

You’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.

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.

Copy and paste this val to create a table:

Table creationRun in Val Town ↗
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();

Use template strings to safely insert data (this prevents SQL injection):

Data insertionRun in Val Town ↗
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();

Get back the data you just inserted:

Data queryRun in Val Town ↗
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();

For dynamic queries, use prepared statements to prevent SQL injection:

Parameterized queryRun in Val Town ↗
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();