Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PGRest - High-Performance PostgreSQL Connection Pooler

PGRest is a cloud-native PostgreSQL connection pooler written in Go, designed as a fully compatible alternative to Supavisor. It provides advanced features like multi-tenant support, read/write splitting, and connection pooling with a RESTful API for management.

Features

  • High Performance: Support for 250,000+ idle connections per instance
  • Multi-Tenant: Secure isolation between different databases/projects
  • Connection Pooling: Transaction, Session, and Proxy modes
  • Read/Write Splitting: Automatic query routing to read replicas
  • Authentication: Full support for SCRAM-SHA-256, MD5, and password auth
  • SSL/TLS: Client and server-side encryption with SNI support
  • RESTful API: OpenAPI-compatible management interface
  • Monitoring: Prometheus metrics and health checks
  • Cloud Native: Kubernetes-ready with horizontal scaling

Quick Start

Installation

# Clone the repository
git clone https://github.com/pgrest/pgrest
cd pgrest

# Build the binary
make build

# Run with default configuration
./bin/pgrest

Docker

# Run with Docker
docker run -p 5432:5432 -p 8080:8080 pgrest/pgrest:latest

# With custom configuration
docker run -p 5432:5432 -p 8080:8080 \
  -e DATABASE_URL="postgres://user:pass@host/db" \
  -e JWT_SECRET="your-secret" \
  pgrest/pgrest:latest

Configuration

PGRest can be configured via environment variables or a configuration file:

# config.yaml
server:
  port: 5432
  api_port: 8080
  ssl_enabled: true

pool:
  default_size: 25
  max_size: 100
  idle_timeout: 900s
  checkout_timeout: 30s

auth:
  jwt_secret: ${JWT_SECRET}
  api_blocklist: []

database:
  url: ${DATABASE_URL}
  max_connections: 10

Architecture

PGRest implements the full PostgreSQL wire protocol, sitting between your applications and PostgreSQL databases:

┌─────────────┐     ┌─────────────┐     ┌─────────────┐
│   Client    │────▶│   PGRest    │────▶│ PostgreSQL  │
└─────────────┘     └─────────────┘     └─────────────┘
                           │
                           ▼
                    ┌─────────────┐
                    │   REST API  │
                    └─────────────┘

API Usage

Create a Tenant

curl -X PUT http://localhost:8080/api/tenants/my-project \
  -H "Authorization: Bearer $JWT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "db_host": "postgres.example.com",
    "db_port": 5432,
    "db_database": "mydb",
    "users": [{
      "db_user": "appuser",
      "db_password": "secret",
      "pool_size": 20,
      "mode_type": "transaction"
    }]
  }'

Connect via PostgreSQL Protocol

# Connect using standard PostgreSQL tools
psql "postgres://appuser.transaction@localhost:5432/mydb"

# Or with any PostgreSQL client library

Performance

PGRest is designed for high performance and scalability:

  • Throughput: Within 90% of direct PostgreSQL connections
  • Latency: Sub-millisecond connection checkout
  • Connections: 250,000+ idle connections per 16-core instance
  • Memory: ~4KB per idle connection

Development

Building from Source

# Install dependencies
go mod download

# Run tests
make test

# Build binary
make build

# Run locally
make run

Project Structure

pgrest/
├── cmd/pgrest/          # Application entry point
├── internal/            # Internal packages
│   ├── api/            # REST API implementation
│   ├── auth/           # Authentication mechanisms
│   ├── pool/           # Connection pool implementation
│   ├── protocol/       # PostgreSQL wire protocol
│   └── tenant/         # Multi-tenant management
├── pkg/                # Public packages
└── test/               # Integration tests

Monitoring

PGRest exposes Prometheus metrics at /metrics:

  • pgrest_connections_active: Active connections by tenant
  • pgrest_connections_total: Total connections created
  • pgrest_pool_checkout_duration: Pool checkout latency
  • pgrest_query_duration: Query execution time
  • pgrest_errors_total: Error count by type

Contributing

We welcome contributions! Please see CONTRIBUTING.md for guidelines.

License

PGRest is licensed under the Apache License 2.0. See LICENSE for details.

Comparison with Supavisor

PGRest maintains full API compatibility with Supavisor while offering:

  • Better Performance: Optimized Go implementation
  • Enhanced Multi-Database Support: Improved routing and cluster management
  • Extended Monitoring: More detailed metrics and tracing
  • Simplified Deployment: Single binary with minimal dependencies

Support

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages