Skip to content

achanda/cloudflare-postgres-containers

Repository files navigation

PostgreSQL + PostgREST on Cloudflare Containers

Deploy to Cloudflare

PostgreSQL + PostgREST Template Preview

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

Features

  • 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

API Endpoints

Health & Schema

  • GET /api/health - Check database health
  • GET /api/schema - Get database schema information

Users

  • GET /api/users - Get all users
  • GET /api/users/:id - Get user by ID
  • POST /api/users - Create a new user
  • PUT /api/users/:id - Update user
  • DELETE /api/users/:id - Delete user

Posts

  • GET /api/posts - Get all posts
  • GET /api/posts/:id - Get post by ID
  • POST /api/posts - Create a new post
  • PUT /api/posts/:id - Update post
  • DELETE /api/posts/:id - Delete post

Load Balanced

  • GET /api/lb/* - Load balanced requests across multiple containers

Getting Started

First, run:

npm install
# or
yarn install
# or
pnpm install
# or
bun install

Then run the development server (using the package manager of your choice):

npm run dev

Open 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.

Database Schema

The application includes two main tables:

Users Table

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
);

Posts Table

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
);

Example Usage

Create a new user

curl -X POST http://localhost:8787/api/users \
  -H "Content-Type: application/json" \
  -d '{"name": "Alice Johnson", "email": "alice@example.com"}'

Get all posts

curl http://localhost:8787/api/posts

Create a new post

curl -X POST http://localhost:8787/api/posts \
  -H "Content-Type: application/json" \
  -d '{"user_id": 1, "title": "My First Post", "content": "Hello world!"}'

Deploying To Production

Command Action
npm run deploy Deploy your application to Cloudflare

Learn More

To learn more about the technologies used, take a look at the following resources:

Your feedback and contributions are welcome!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published