# Connect Postgres for Gin

## Summary
Database connection for a Gin app on Ample. Verified on Gin: a managed PostgreSQL database auto-provisioned and injected as DATABASE_URL, with real queries succeeding at request time (records=ok). Build and start: `CGO_ENABLED=0 go build -o app ./...` then `./app` on the ubuntu-24.04 template (go.sum committed for a reproducible build).

## Prerequisites
- A Gin project (`CGO_ENABLED=0 go build -o app ./...` then `./app` on the ubuntu-24.04 template (go.sum committed for a reproducible build))
- A PostgreSQL driver reading DATABASE_URL at runtime
- An Ample account token with `servers:write`, `databases:read`

## Resource Requirements
- `primitive:compute`
- `primitive:postgres`

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

## Workflow Steps
1. ### Build and start
   Build: `CGO_ENABLED=0 go build -o app ./...` then `./app` on the ubuntu-24.04 template (go.sum committed for a reproducible build); the server must bind 0.0.0.0 on PORT.

2. ### Read DATABASE_URL at runtime
   Open connections from the environment inside handlers or a pool; omit DATABASE_URL to auto-provision a managed database or pass `--env DATABASE_URL=...` for an existing one.

3. ### Verify
   Fetch the live URL and `/p/relational-records` on the example; on failure read the build and runtime logs.
   Command: `ample logs --kind build`

## Examples
- **Gin pattern fixture**: Verified database connection on Gin.
  Source: `tests/deploy-canaries/gin-patterns`

## Success Checks
- **Check**: App responds on its public URL
  - Kind: http_get
  - Path: `/`
  - Expect: `ample canary gin patterns`

- **Check**: Relational-records check from the example
  - Kind: http_get
  - Path: `/p/relational-records`
  - Expect: `see the pattern fixture checks`

## Limitations
- Verified on the ubuntu-24.04 template at s-1vcpu-1gb; other sizes and Gin major versions are not verified.
- Region, compliance attestations, and request-duration limits are unknown and not claimed.
- Managed PostgreSQL 16 only; extensions, connection limits and backup or restore procedures are not verified.

## Cost Estimate
- **Currency**: USD
- **Monthly Amount**: $10.00
- **Basis**: Size prices from pricing.toml (loaded by the API) at build revision 1ac5595375130d45290090e82ed0f554ccd45405-dirty
- **Components**:
  - App server: s-1vcpu-1gb - $5.00
  - Managed PostgreSQL database: s-1vcpu-1gb - $5.00
- **Note**: Always-on monthly price of the tested sizes; apps auto-pause when idle.

## Evidence Summary
Kind: canary_run  
Summary: Gin pattern fixture deployed on Ample (CGO_ENABLED=0 go build -o app ./... then ./app on the ubuntu-24.04 template (go.sum committed for a reproducible build)); for this guide: a managed PostgreSQL database auto-provisioned and injected as DATABASE_URL, with real queries succeeding at request time (records=ok).
Observed At: 2026-09-21T01:15:54Z
Impl. Revision: 50dbac567d2c5cc8e55e6c748a646be0025d9cfb-dirty (CLI 0.1.21)
Expires At: 2027-03-20T01:15:54Z
Scope: checks: `/p/relational-records`, template: `ubuntu-24.04`

## Formats
- HTML: [https://ample.computer/guides/gin/database-connection](/content/guides/gin/database-connection/index.html)
- Markdown: [https://ample.computer/guides/gin/database-connection.md](/content/guides/gin/database-connection.md)
- JSON: [https://api.ample.computer/v1/catalog/recipes/page%3Aguides%3Agin%3Adatabase-connection](https://api.ample.computer/v1/catalog/recipes/page%3Aguides%3Agin%3Adatabase-connection)
