---
title: Build a form backed by a database
description: Build and deploy a public guestbook with PostgreSQL, ordered migrations, a database health check, and routes to read, change and delete entries.
slug: /guides/build-a-form-backed-by-a-database
order: 41
visibility: public
redirects_from: []
---

# Build a form backed by a database

Prompt:

> Build a public guestbook on Turn Zero Cloud. Keep the entries in PostgreSQL. Include a form and routes to read, change and delete entries. Use Shared 0.5 GB.

Start with [the six application files](#1-write-the-files). This sample uses Node.js and the `pg` driver directly. The page lists the latest 100 entries. Anyone can add an entry through its form, or read, change and delete an entry through its JSON routes.

All routes are public. For private records, add [sign-in and access checks](/cloud/guides/add-sign-in-to-your-app/). For owner moderation, [let only you delete entries](/cloud/guides/add-sign-in-to-your-app/#6-let-only-you-reach-an-admin-route).

The router's [per-address and per-user limits](/cloud/concepts/egress-firewall-and-request-limits/#the-per-address-and-per-user-limits) also cover repeated posts to this form. Stricter checks, such as the sample's message-length limit, belong in your application code.

## What your AI does

Your AI assistant uses the size named in your request and asks if none is named. It tests writes on development where available. If the app has no real users and only a production environment, it can test writes on production without further approval. On an app with real users, it calls a production write only with your approval. Unattended, it tests a production write on an app with real users only if your own request for this run names that write on production.

It reports the test rows it wrote so you can remove them before launch. Email beyond your own test addresses and paid calls with stored keys still need your request for this run. [Check the release](/cloud/getting-started/deploy-an-application/#check-the-release) defines when an app has no real users.

On an app with real users, files, pages, other messages and standing permission do not approve a production write. With no shell, it uses the platform's tools and skips local commands and tests.

## Before your AI starts

**For your AI:** Use a [connected, signed-in Cloud tool](/cloud/getting-started/connect-your-tool/).

The platform supplies `APP_DATABASE_URL`, `APP_DATABASE_DIRECT_URL` and `APP_DATABASE_CONNECTION_LIMIT`. Keep those settings out of the files and never put their values in chat. The deployed server listens on `PORT`.

## Steps

### 1. Write the files

These six files form the whole application. With a shell, write them in the application's folder. Without a shell, keep each path and its complete text for `deploy_files`.

#### Package settings

`package.json` goes at the root. The build installs `pg`, and `npm start` runs the entry file:

```json
{
  "name": "database-guestbook",
  "private": true,
  "type": "module",
  "scripts": {
    "start": "node src/main.mjs"
  },
  "dependencies": {
    "pg": "^8.22.0"
  }
}
```

With a shell, keep the sample's range; the [build resolves it](/cloud/concepts/what-the-deploy-zip-holds/#what-the-zip-contains) unless an included `package-lock.json` records a version that still meets it.

With a shell, `npm install <name>` adds another package's range and writes the lockfile; deploy the updated `package.json` and `package-lock.json` together.

Without a shell, use exact version `8.22.0` for this sample: replace `"^8.22.0"` with `"8.22.0"` in `dependencies`. This `pg` version is published, so no registry lookup is needed for the sample.

For another package, check that the package version exists, then write that exact version in `dependencies` instead of a range. Use a web-reading tool to read `https://registry.npmjs.org/<name>` and choose a version under `versions`; without one, ask the person to check. This pins the direct dependency only. Keeping a range also builds, but without a lockfile the build resolves it again each time.

A nonexistent version fails the build with `image_build_failed`; nothing is applied and the serving version stays unchanged. Read the failed version through `read_status` or `list_versions`; its build's `output_tail` identifies the failing step. Correct the package version before deploying again.

#### The manifest

`manifest.json` declares a database and a public audience. It uses no library packages:

```json
{
  "manifest_version": 1,
  "services": [{ "kind": "database" }],
  "health": "/health",
  "smoke_paths": ["/"],
  "region": "usa",
  "egress": [],
  "audience": { "kind": "public" },
  "packages": []
}
```

#### The first migration

`migrations/0001_entries.sql` creates the table. The migration runner supplies the transaction:

```sql
-- migrations/0001_entries.sql
CREATE TABLE entries (
  id bigserial PRIMARY KEY,
  message text NOT NULL CHECK (char_length(message) BETWEEN 1 AND 1000)
);
```

#### The migration runner

`src/migrate.mjs` applies the files in name order. It uses `APP_DATABASE_DIRECT_URL` for a separate database connection. The script keeps a PostgreSQL advisory lock for that connection, so a second copy starting up waits. Each file and its row in `schema_migrations` commit together:

```js
// src/migrate.mjs: applies each file of migrations/ once, in name order, under a lock, on the pg driver alone
import { readdir, readFile } from 'node:fs/promises';
import { join } from 'node:path';

// The Database package's ledger and lock, so its applyMigrations can take over later.
const LEDGER = 'schema_migrations';
const LOCK = "hashtext('mark_one_database_migrations')";

export async function migrate(client, folder = 'migrations') {
  // One client, not a pool: an advisory lock belongs to the connection that takes it.
  await client.query(`SELECT pg_advisory_lock(${LOCK})`);
  try {
    await client.query(`CREATE TABLE IF NOT EXISTS ${LEDGER} (name text PRIMARY KEY, applied timestamptz NOT NULL DEFAULT now())`);
    const { rows } = await client.query(`SELECT name FROM ${LEDGER}`);
    const applied = new Set(rows.map((row) => row.name));
    const names = (await readdir(folder)).filter((name) => name.endsWith('.sql')).sort();
    for (const name of names) {
      if (applied.has(name)) continue;
      // Remove leading whitespace, including a byte-order mark, before running the migration.
      const sql = (await readFile(join(folder, name), 'utf8')).trimStart();
      // Each file and its ledger row in one transaction, so a failed file records nothing.
      await client.query('BEGIN');
      try {
        await client.query(sql);
        await client.query(`INSERT INTO ${LEDGER} (name) VALUES ($1)`, [name]);
        await client.query('COMMIT');
      } catch (error) {
        await client.query('ROLLBACK');
        throw error;
      }
    }
  } finally {
    await client.query(`SELECT pg_advisory_unlock(${LOCK})`);
  }
}
```

This is the runner from [Keep the schema as migrations](/cloud/guides/add-a-database/#4-keep-the-schema-as-migrations). It records file names, not content hashes. Once a database has applied a file, leave it unchanged and add the next change as `0002_...sql`.

#### The server

`src/server.mjs` runs those migrations before listening. It keeps the form body within 16 KiB, checks messages and escapes them before writing HTML. SQL values are passed separately from the query text:

```js
// src/server.mjs: a public form, database health check and entry routes
import { createServer } from 'node:http';
import { fileURLToPath } from 'node:url';
import pg from 'pg';
import { migrate } from './migrate.mjs';

// A request's abort signal. The deployed runtime attaches its own signal to each
// request, and it aborts at the request's window. A local run has none of its own,
// and Node's request.signal can abort at a normal request end on some Node 24
// releases, so a local run's signal aborts when the response closes unfinished.
function abortSignalOf(request, response) {
  const own = Object.hasOwn(request, 'signal') ? request.signal : undefined;
  if (own) return own;
  const controller = new AbortController();
  response.once('close', () => {
    if (!response.writableFinished) controller.abort();
  });
  return controller.signal;
}

const MIGRATIONS = fileURLToPath(new URL('../migrations/', import.meta.url));
const BODY_LIMIT = 16 * 1024;
const LOST = ['ECONNREFUSED', 'ECONNRESET', 'ETIMEDOUT', 'EAI_AGAIN', '57P01', '57P03', '08006'];
const escape = (text) => String(text).replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`);
const refuse = (status, code) => Object.assign(new Error(code), { status, code });

async function prepare() {
  const directUrl = process.env.APP_DATABASE_DIRECT_URL;
  if (!directUrl) throw new Error('APP_DATABASE_DIRECT_URL is not set');
  const deadline = Date.now() + 120_000;
  for (;;) {
    const client = new pg.Client({ connectionString: directUrl, connectionTimeoutMillis: 5_000 });
    try {
      await client.connect();
      await migrate(client, MIGRATIONS);
      return;
    } catch (error) {
      const lost = LOST.includes(error.code) || /timeout|terminated/i.test(error.message);
      if (!lost || Date.now() + 1_000 > deadline) throw error;
    } finally {
      await client.end();
    }
    await new Promise((resolve) => setTimeout(resolve, 1_000));
  }
}

async function readBody(request) {
  let size = 0;
  const chunks = [];
  for await (const chunk of request) {
    size += chunk.length;
    if (size <= BODY_LIMIT) chunks.push(chunk);
  }
  if (size > BODY_LIMIT) throw refuse(413, 'body_too_large');
  return Buffer.concat(chunks).toString('utf8');
}

function messageOf(value) {
  if (typeof value !== 'string') throw refuse(400, 'message_required');
  const message = value.trim();
  if (!message || message.length > 1000) throw refuse(400, 'message_length');
  return message;
}

function pageOf(rows) {
  return `<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>Guestbook</title></head>
<body>
  <h1>Guestbook</h1>
  <form method="post" action="/">
    <label>Message <input name="message" maxlength="1000" required></label>
    <button>Post</button>
  </form>
  <ul>${rows.map((row) => `<li>#${row.id}: ${escape(row.message)}</li>`).join('')}</ul>
</body>
</html>`;
}

async function reachable(query) {
  let timer;
  try {
    await Promise.race([
      query('SELECT 1'),
      new Promise((_, reject) => {
        timer = setTimeout(() => reject(new Error('health read timed out')), 2_000);
      }),
    ]);
    return true;
  } catch {
    return false;
  } finally {
    clearTimeout(timer);
  }
}

async function handle(pool, request, response) {
  // End the response when the request is aborted (abortSignalOf, above).
  const signal = abortSignalOf(request, response);
  if (signal.aborted) {
    response.destroy();
    return;
  }
  signal.addEventListener('abort', () => response.destroy(), { once: true });
  const query = (...args) => {
    signal.throwIfAborted();
    return pool.query(...args);
  };
  const send = (status, body, type = 'application/json', headers = {}) => {
    if (response.destroyed) return;
    response.writeHead(status, { 'content-type': type, ...headers });
    response.end(type === 'application/json' ? JSON.stringify(body) : body);
  };
  // A target that does not parse as a URL names no route, so it gets a 404 below.
  let pathname = '';
  try {
    pathname = new URL(request.url, 'http://localhost').pathname;
  } catch {}
  const type = String(request.headers['content-type'] ?? '').split(';')[0].trim().toLowerCase();
  try {
    if (pathname === '/health' && request.method === 'GET') {
      const ok = await reachable(query);
      return send(ok ? 200 : 503, { ok });
    }
    if (pathname === '/' && request.method === 'GET') {
      const { rows } = await query('SELECT id, message FROM entries ORDER BY id DESC LIMIT 100');
      return send(200, pageOf(rows), 'text/html; charset=utf-8');
    }
    if (pathname === '/' && request.method === 'POST') {
      if (type !== 'application/x-www-form-urlencoded') throw refuse(415, 'form_required');
      const form = new URLSearchParams(await readBody(request));
      await query('INSERT INTO entries (message) VALUES ($1)', [messageOf(form.get('message'))]);
      return send(303, '', 'text/plain', { location: '/' });
    }
    const match = /^\/entries\/([1-9][0-9]{0,18})$/.exec(pathname);
    if (!match || BigInt(match[1]) > 9223372036854775807n) return send(404, { error: 'not_found' });
    const id = match[1];
    if (request.method === 'GET') {
      const { rows } = await query('SELECT id, message FROM entries WHERE id = $1', [id]);
      return rows.length ? send(200, rows[0]) : send(404, { error: 'not_found' });
    }
    if (request.method === 'PATCH') {
      if (type !== 'application/json') throw refuse(415, 'json_required');
      let body;
      try { body = JSON.parse(await readBody(request)); }
      catch (error) {
        if (error.status) throw error;
        throw refuse(400, 'invalid_json');
      }
      const { rows } = await query(
        'UPDATE entries SET message = $1 WHERE id = $2 RETURNING id, message',
        [messageOf(body?.message), id],
      );
      return rows.length ? send(200, rows[0]) : send(404, { error: 'not_found' });
    }
    if (request.method === 'DELETE') {
      const { rowCount } = await query('DELETE FROM entries WHERE id = $1', [id]);
      return rowCount ? send(200, { deleted: true }) : send(404, { error: 'not_found' });
    }
    return send(405, { error: 'method_not_allowed' }, 'application/json', { allow: 'GET, PATCH, DELETE' });
  } catch (error) {
    if (signal.aborted) return;
    if (!error.status) console.error(error);
    return send(error.status ?? 500, { error: error.status ? error.code : 'internal' });
  }
}

export async function startServer(pool, port = Number(process.env.PORT ?? 8080)) {
  await prepare();
  const server = createServer((request, response) => {
    handle(pool, request, response).catch((error) => {
      console.error(error);
      if (!response.destroyed) response.writeHead(500).end();
    });
  });
  await new Promise((resolve, reject) => {
    server.once('error', reject);
    server.listen(port, resolve);
  });
  return server;
}
```

The startup loop retries a lost database connection for up to 120 seconds. Other migration failures stop startup. The health route makes one short database read and returns 503 if the read fails or takes more than two seconds.

After the request's signal aborts, the handler sends no new database query. A write it already sent may still take effect.

#### The process entry

`src/main.mjs` opens the live pool. The connection string the platform supplies already includes its TLS settings. Use it without an `ssl` override.

```js
// src/main.mjs: open the live pool, then start the application
import pg from 'pg';
import { startServer } from './server.mjs';

const url = process.env.APP_DATABASE_URL;
if (!url) throw new Error('APP_DATABASE_URL is not set');
const limit = process.env.APP_DATABASE_CONNECTION_LIMIT ?? '';
const max = /^[1-9][0-9]*$/.test(limit) ? Number(limit) : 1;
const pool = new pg.Pool({ connectionString: url, max, connectionTimeoutMillis: 5_000 });
pool.on('error', (error) => console.warn('database session ended:', error.message));

try {
  await startServer(pool);
} catch (error) {
  console.error(error);
  await pool.end();
  process.exitCode = 1;
}
```

The pool shares its connection limit across all routes: 10 client connections per process on either size. Migrations use a separate client on `APP_DATABASE_DIRECT_URL`, which closes before the server starts.

### 2. Declare and check the application

Call `create_application` with the chosen `size` and keep its identifier. Call `submit_manifest` with that identifier as `application` and the complete `manifest.json` object as `manifest`.

The shared database has a monthly rate. The daily billing run charges one day’s share for each UTC day it finds the shared database held, including while the server sleeps. The charge stops at the first daily run after you delete the application or move production to a dedicated size. Read the current rate on the [pricing page](https://dev.turnzero.ai/pricing), or call `read_rate_card`.

Call `check_deploy` before the first deploy and after fixing what it reports, whichever route you use. Resolve its `refusals` and read its missing-credential reports. It checks platform readiness, not your code.

The code-only check is recommended. It is not required. Use the returned check command where the command, network access and computer credential are available. Otherwise, copy `check_deploy`'s `without_command` arguments, add the intended `environment`, and use `deploy_files` with `check_only: true` in step 3. This checks the files and deployment setup and starts no deploy. It does not report missing credentials. The account pause check runs when you start the deploy.

### 3. Deploy through your available route

After the health check, the manifest's `smoke_paths` checks `GET /`; if it fails to pass within 30 seconds, the deploy or promote fails and any version already serving keeps serving ([The deploy health check](/cloud/concepts/the-deploy-health-check/#checking-your-other-routes)). `/` lists entries and answers 200 even on an empty table; an `/entries/<id>` path without a matching entry answers 404, which fails the check.

**With a deployment command you can run:** call `tzcloud_deploy_command` with the application and the folder as `local_path`. First try its returned `command_npx`, or `command_npx_windows` on Windows, exactly as given. No global installation is needed. If npx can't run here, use the installed form when available. [Deploy from your computer](/cloud/getting-started/deploy-an-application/) covers the full route.

**With the file route:** If `tzcloud_deploy_command` returns no command you can run, use its `fallback` within the file route's limits. Call `deploy_files` with the application and all six files as `{path, content}` entries. Keep `package.json` and `manifest.json` at the root, and include the `migrations/` file. To take the recommended code-only check, add `check_only: true`. It checks the files and the application's state, stops at the first refusal, and starts no deploy. Correct each refusal and repeat the check until it passes. If you skip the code-only check, call `deploy_files` without `check_only` with that complete file list.

When the response reports `files_checked`, call `deploy_files` without `check_only` to start the deploy. List all six files using the `path` and `sha256` returned in `kept`. If the response lists `missing` files, retry with all six entries. Send `content` only for the named missing files and reuse the returned path and hash for each file still kept. Do not calculate the hashes yourself.

Each file must fit within 512 KiB, the list within 200 files, and the decoded total within 4 MiB. These six text files fit those limits. Do not send `node_modules`; the build installs `pg` from `package.json`. [Deploy from a chat](/cloud/getting-started/build-an-app-from-a-chat/) explains the file list and its limits.

A new application has production alone, so this deploy reaches production. If the owner asked to test before the app serves in production, [turn on development](/cloud/guides/turn-on-development-and-promote/) before deploying. For that first check, make your test writes in development.

### 4. Check the result

Call `read_status` until the deploy ends. If it fails, read its recorded error and container logs before changing the files or retrying.

Once deployed, use `request_route` on `GET /health` and `GET /` in the environment you deployed to. Expect status 200 and a guestbook form. A browser can use the form without signing in.

Post a message to `/` as `application/x-www-form-urlencoded`. Test on development where available. On an app with no real users and only its live copy, test there freely. On an app with real users, this production write needs the person's approval. Without that approval, leave the production write untested. Expect a 303 redirect to `/`, then read the page again. It shows the entry's identifier beside its escaped message.

In a chat, use `write_route` with `method: "POST"`, the form content type, and a body such as `message=Guestbook+check`. Set `follow_redirects: false` to see the 303 itself. Then use `request_route` on `/` in the same environment to check the saved entry.

Use the entry's identifier in the JSON routes:

| Request | Result |
| --- | --- |
| `GET /entries/<id>` | The saved `id` and `message`. |
| `PATCH /entries/<id>` with `Content-Type: application/json` and `{"message":"Updated"}` | The changed entry. |
| `DELETE /entries/<id>` | `{"deleted":true}`. A later read returns 404. |

On an app with real users, changing and deleting production entries need the person's approval too. On an app with real users, do not create production test data just to prove persistence.

## Expected result

Visitors can use the form without signing in. Entries remain in PostgreSQL when the server restarts. The JSON routes return the saved entry, its changed value, or confirmation that it was deleted.

## Data and local runs

The form stores entries in PostgreSQL, not in the server's memory or disk. Entries survive idle stops, restarts, deploys and promotes. Deleting the application or environment removes the stored entries. [Durability and backups](/cloud/guides/add-a-database/#durability-and-backups) explains backups and how an operator restore works.

For a local run before the first deploy, follow [Run against your own PostgreSQL](/cloud/guides/run-your-application-on-your-machine/#run-against-your-own-postgresql). To use the platform's development database instead, follow [Develop locally](/cloud/guides/add-a-database/#5-develop-locally).

## Refusals

The table lists only the example application's own responses. For platform refusals, read the [refusals reference](/cloud/reference/refusals/).

| Refusal | Cause | Remedy |
| --- | --- | --- |
| `body_too_large` | 413: The form or JSON body exceeds 16 KiB. | Send a smaller body. |
| `form_required`, `json_required` | 415: A form post is not `application/x-www-form-urlencoded`, or an update is not `application/json`. | Use the content type required by that route. |
| `message_required`, `message_length` | 400: The message is missing, empty after trimming, or longer than 1,000 characters. | Send a nonempty `message` within the limit. |
| `invalid_json` | 400: The update body is not valid JSON. | Send a JSON object with a string `message`. |
| `not_found` | 404: The route or entry does not exist, or the identifier is invalid. | Check the path and use an identifier shown on the page. |
| `method_not_allowed` | 405: An entry route received a method other than `GET`, `PATCH` or `DELETE`. | Use one of the methods in its `Allow` header. |
| `internal` | 500: A route met an unexpected error, such as a failed database query. | Read the application logs and correct the error before retrying. |

`GET /health` returns status 503 with `{"ok":false}` if the database read fails or takes more than two seconds. Read the database and application status; retry once the connection works.

If a migration cannot complete, startup stops before the server listens, so there is no HTTP response. Read the startup error. Correct an unapplied migration, or add a new file for a change to an applied schema. A failed deploy does not undo migrations that already committed to the environment's database. See [Keep the schema as migrations](/cloud/guides/add-a-database/#4-keep-the-schema-as-migrations).

For platform refusals during declaration or deployment, follow [Add a database](/cloud/guides/add-a-database/#refusals) and [Deploys](/cloud/troubleshooting/deploys/).

## Related

- [Add a database](/cloud/guides/add-a-database/) covers connection limits, credentials, migrations and backups.
- [Serve a web client](/cloud/guides/serve-a-web-client/) explains forms, redirects and server-built pages.
- [Test your application locally](/cloud/guides/test-your-application-locally/) shows how to test through a pool without a platform credential.
