This is a Container starter template that runs PostgreSQL with PostgREST.
It demonstrates:
- Running PostgreSQL database in a Cloudflare Container
- Using PostgREST to automatically generate REST API endpoints
- Managing database connections and container lifecycle
- Load balancing across multiple database instances
- Full CRUD operations through REST API
- PostgreSQL Database: Full-featured PostgreSQL database running in containers
- PostgREST API: Automatic REST API generation for database tables
- Container Management: Automatic container lifecycle management with Cloudflare Workers
- Load Balancing: Distribute requests across multiple database instances
- Health Checks: Built-in health monitoring for database availability
- Sample Data: Pre-configured users and posts tables with sample data
GET /api/health- Check database healthGET /api/schema- Get database schema information
GET /api/users- Get all usersGET /api/users/:id- Get user by IDPOST /api/users- Create a new userPUT /api/users/:id- Update userDELETE /api/users/:id- Delete user
GET /api/posts- Get all postsGET /api/posts/:id- Get post by IDPOST /api/posts- Create a new postPUT /api/posts/:id- Update postDELETE /api/posts/:id- Delete post
GET /api/lb/*- Load balanced requests across multiple containers
First, run:
npm install
# or
yarn install
# or
pnpm install
# or
bun installThen run the development server (using the package manager of your choice):
npm run devOpen http://localhost:8787 with your browser to see the result.
You can start editing your Worker by modifying src/index.ts and you can start
editing your Container by editing the Dockerfile and related configuration files.
The application includes two main tables:
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);CREATE TABLE posts (
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
title VARCHAR(200) NOT NULL,
content TEXT,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);curl -X POST http://localhost:8787/api/users \
-H "Content-Type: application/json" \
-d '{"name": "Alice Johnson", "email": "alice@example.com"}'curl http://localhost:8787/api/postscurl -X POST http://localhost:8787/api/posts \
-H "Content-Type: application/json" \
-d '{"user_id": 1, "title": "My First Post", "content": "Hello world!"}'| Command | Action |
|---|---|
npm run deploy |
Deploy your application to Cloudflare |
To learn more about the technologies used, take a look at the following resources:
- Container Documentation - learn about Cloudflare Containers
- PostgREST Documentation - learn about PostgREST
- PostgreSQL Documentation - learn about PostgreSQL
Your feedback and contributions are welcome!