← Back to Recipes

Store in Database Guide

Persist every form submission to your own PostgreSQL database.

1. Create the Submissions Table

Run this migration once against your PostgreSQL database:

-- Run once to create the submissions table
CREATE TABLE form_submissions (
  id          SERIAL PRIMARY KEY,
  submitted_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  data        JSONB        NOT NULL
);

2. Install Dependencies

npm install pg

3. Create the Handler

Deploy this as a serverless function or Node.js endpoint. It forwards the submission to Peach Form and then inserts it into your database.

// handler.js — deploy to Vercel, Netlify, or any Node.js server
import { Pool } from "pg";

const pool = new Pool({ connectionString: process.env.DATABASE_URL });

export default async function handler(req, res) {
  if (req.method !== "POST") {
    return res.status(405).end("Method not allowed");
  }

  const body = req.body; // parsed JSON

  // 1. Forward submission to Peach Form
  const pfRes = await fetch("https://api.peachform.com/f/YOUR_FORM_ID", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(body),
  });

  if (!pfRes.ok) {
    return res.status(502).json({ error: "Peach Form submission failed" });
  }

  // 2. Store submission in your own database
  await pool.query(
    "INSERT INTO form_submissions (data) VALUES ($1)",
    [JSON.stringify(body)],
  );

  return res.status(200).json({ success: true });
}

4. Set Environment Variables

DATABASE_URL=postgres://user:pass@localhost:5432/mydb

Expected Output

-- After submitting, query your table:
SELECT id, submitted_at, data
FROM form_submissions
ORDER BY submitted_at DESC
LIMIT 1;

--  id | submitted_at               | data
-- ----+----------------------------+----------------------------------------------
--   1 | 2026-04-28 12:00:00+00:00  | {"name":"Jane Doe","email":"[email protected]","message":"Hello!"}