# Connect Postgres for Next.js

## Summary

Connect a Next.js app to PostgreSQL on Ample from a route handler. Read DATABASE_URL at request time (force-dynamic) with pg; with no DATABASE_URL supplied, Ample auto-provisions a managed PostgreSQL 16 database and injects its connection string as an encrypted environment variable.

## Infrastructure requirements

- Compute: verified (Apps run in isolated x86_64 Firecracker microVMs that auto-pause when idle and wake on request; sizes are the priced VM sizes.)
- Postgres: verified (Managed PostgreSQL 16 runs in its own microVM and is auto-provisioned when an app needs a database and no DATABASE_URL is supplied.)

## Prerequisites

- The pg driver in dependencies and a route handler that reads process.env.DATABASE_URL at request time
- Either a production DATABASE_URL to pass with --env, or nothing (auto-provision)
- An Ample account token with servers:write and databases:read

## Exact tested configuration

- template: `node-22`
- runtime: `node`
- size: `s-1vcpu-1gb`
- install: `npm install`
- build: `npm run build --if-present`
- start: `npm run start`

## Steps

1. **Keep the database out of the build.** Mark the route handler dynamic and open the client per request; next build must not need the database.
2. **Deploy.** Omit DATABASE_URL to auto-provision, or pass --env DATABASE_URL=... to use an existing database.
   
   ```
   ample deploy . --name <app-name> --public
   ```
3. **Test through the app.** Fetch the route; it should report postgres=ok after a real query.
4. **Verify.** Fetch the live URL and run the success checks below. On failure read the build log, then the runtime log, fix the cause and deploy again; do not blind-retry.
   
   ```
   ample logs <deployment_id> --kind build
   ```

## Tested examples

- **Next.js + pg canary** (tests/deploy-canaries/next-postgres-basic): Route handler /api/db with an auto-provisioned managed database.

## Success checks

- database route reports a live connection (`/api/db` on the live URL, expect postgres=ok)

## Limitations

- Verified on the node-22 template at s-1vcpu-1gb; other templates and sizes are not verified by this recipe.
- Region, compliance attestations and request-duration limits are unknown and not claimed.
- Apps auto-pause when idle and wake on the next request; always-on is an operator setting, not a plan feature.
- Managed PostgreSQL 16 only; extensions, connection limits and backup or restore procedures are not verified.
- Apps and their managed databases are placed together; cross-node shared databases are unsupported.
- Connection pooling across serverless-style invocations is not applicable; the app is one long-lived process.

## Cost estimate

Estimated 10.00 USD per month (size prices from pricing.toml at build revision a1b8c38919e59cd035ebabaced73cf84ece24371).
- app server x1 `s-1vcpu-1gb`: 5.00 USD
- managed PostgreSQL database x1 `s-1vcpu-1gb`: 5.00 USD

Apps and managed databases auto-pause when idle; the estimate is the always-on monthly price of the tested sizes. Plan quotas and budgets apply.

## Verification evidence

- canary_run on 2026-09-20T01:42:25Z at revision `199ff1dfd52683832ae75d3f98b53a7a4bff7f96-dirty (CLI e3181f5)`: Next.js 15 route handler using pg with no DATABASE_URL supplied: Ample auto-provisioned a managed PostgreSQL database and /api/db reported postgres=ok. (expires 2027-03-19T01:42:25Z)
- canary_run on 2026-09-19T23:55:33Z at revision `1764076-tls-account-903d345`: Node app needing PostgreSQL with no DATABASE_URL: a managed database was auto-provisioned and /db reported postgres=ok. (expires 2027-03-18T23:55:33Z)

Last verified: 2026-09-20T01:42:25Z

## Execution binding

MCP tool `ample_deploy` (registry `mcp:ample_deploy`), schema hash `876465fce906da0c` observed 2026-09-20T01:55:48.667900+00:00 at revision `199ff1dfd526`, binding state `current`, required scopes: servers:write, databases:read.
