docker.recipes

Go Fiber + PostgreSQL

intermediate

Go Fiber web framework with PostgreSQL.

Overview

Go is a statically typed, compiled programming language developed by Google in 2009, designed for simplicity, efficiency, and excellent concurrency support. Go Fiber is an Express-inspired web framework built on top of Fasthttp, the fastest HTTP engine for Go, emphasizing zero memory allocation and exceptional performance. This makes it ideal for building high-throughput APIs and web services that can handle thousands of concurrent connections with minimal resource overhead. This stack combines Go Fiber's lightning-fast HTTP processing with PostgreSQL's robust relational database capabilities, creating a powerful foundation for data-intensive web applications. Go Fiber handles HTTP routing, middleware, and request processing with minimal latency, while PostgreSQL provides ACID-compliant data storage with advanced features like JSON support, full-text search, and complex query capabilities. The combination leverages Go's excellent database/sql package ecosystem, typically through GORM or sqlx, for type-safe database operations. Developers building REST APIs, microservices, or web applications requiring both speed and data integrity should consider this stack. Startups needing rapid development with production-ready performance, enterprises requiring reliable data transactions, and teams familiar with SQL who want the performance benefits of Go will find this combination particularly valuable. The stack excels in scenarios demanding high concurrent user loads while maintaining complex relational data relationships.

Key Features

  • Zero memory allocation HTTP router with Express-inspired API design
  • Fasthttp engine providing 10x faster performance than standard Go net/http
  • ACID compliance with advanced PostgreSQL transaction support and isolation levels
  • Built-in middleware support for authentication, CORS, logging, and request validation
  • PostgreSQL JSON/JSONB columns for hybrid relational-document data models
  • Advanced SQL capabilities including window functions, CTEs, and table partitioning
  • Go's native concurrency with goroutines handling thousands of simultaneous database connections
  • PostgreSQL full-text search with ranking and highlighting for content-heavy applications

Common Use Cases

  • 1High-performance REST APIs serving mobile applications with complex data relationships
  • 2E-commerce platforms requiring ACID transactions for inventory and payment processing
  • 3Real-time analytics dashboards with PostgreSQL's advanced aggregation functions
  • 4Content management systems leveraging PostgreSQL's full-text search capabilities
  • 5Financial applications where data integrity and transaction safety are critical
  • 6Multi-tenant SaaS applications using PostgreSQL's row-level security features
  • 7IoT data collection services requiring fast ingestion with relational data analysis

Prerequisites

  • Docker and Docker Compose installed with at least 1.5GB available RAM
  • Go development knowledge including struct tags and error handling patterns
  • PostgreSQL/SQL experience for database schema design and query optimization
  • Port 3000 available for the Fiber application HTTP server
  • Understanding of database connection pooling and Go's database/sql package
  • Familiarity with environment variable configuration for database credentials

For development & testing. Review security settings, change default credentials, and test thoroughly before production use. See Terms

docker-compose.yml

docker-compose.yml
1services:
2 app:
3 build: .
4 container_name: go-fiber
5 environment:
6 DATABASE_URL: postgres://${DB_USER}:${DB_PASSWORD}@postgres:5432/${DB_NAME}?sslmode=disable
7 ports:
8 - "3000:3000"
9 depends_on:
10 - postgres
11 networks:
12 - gofiber
13
14 postgres:
15 image: postgres:16-alpine
16 container_name: postgres
17 environment:
18 POSTGRES_DB: ${DB_NAME}
19 POSTGRES_USER: ${DB_USER}
20 POSTGRES_PASSWORD: ${DB_PASSWORD}
21 volumes:
22 - postgres_data:/var/lib/postgresql/data
23 networks:
24 - gofiber
25
26volumes:
27 postgres_data:
28
29networks:
30 gofiber:
31 driver: bridge

.env Template

.env
1DB_NAME=goapp
2DB_USER=gouser
3DB_PASSWORD=changeme

Usage Notes

  1. 1Docs: https://docs.gofiber.io/
  2. 2Create multi-stage Dockerfile (go build + scratch/alpine)
  3. 3Access API at http://localhost:3000
  4. 4Use golang-migrate for database migrations
  5. 5Fiber: Express-inspired, zero allocation, fast HTTP
  6. 6GORM or sqlx for database access

Individual Services(2 services)

Copy individual services to mix and match with your existing compose files.

app
app:
  build: .
  container_name: go-fiber
  environment:
    DATABASE_URL: postgres://${DB_USER}:${DB_PASSWORD}@postgres:5432/${DB_NAME}?sslmode=disable
  ports:
    - "3000:3000"
  depends_on:
    - postgres
  networks:
    - gofiber
postgres
postgres:
  image: postgres:16-alpine
  container_name: postgres
  environment:
    POSTGRES_DB: ${DB_NAME}
    POSTGRES_USER: ${DB_USER}
    POSTGRES_PASSWORD: ${DB_PASSWORD}
  volumes:
    - postgres_data:/var/lib/postgresql/data
  networks:
    - gofiber

Quick Start

terminal
1# 1. Create the compose file
2cat > docker-compose.yml << 'EOF'
3services:
4 app:
5 build: .
6 container_name: go-fiber
7 environment:
8 DATABASE_URL: postgres://${DB_USER}:${DB_PASSWORD}@postgres:5432/${DB_NAME}?sslmode=disable
9 ports:
10 - "3000:3000"
11 depends_on:
12 - postgres
13 networks:
14 - gofiber
15
16 postgres:
17 image: postgres:16-alpine
18 container_name: postgres
19 environment:
20 POSTGRES_DB: ${DB_NAME}
21 POSTGRES_USER: ${DB_USER}
22 POSTGRES_PASSWORD: ${DB_PASSWORD}
23 volumes:
24 - postgres_data:/var/lib/postgresql/data
25 networks:
26 - gofiber
27
28volumes:
29 postgres_data:
30
31networks:
32 gofiber:
33 driver: bridge
34EOF
35
36# 2. Create the .env file
37cat > .env << 'EOF'
38DB_NAME=goapp
39DB_USER=gouser
40DB_PASSWORD=changeme
41EOF
42
43# 3. Start the services
44docker compose up -d
45
46# 4. View logs
47docker compose logs -f

One-Liner

Run this command to download and set up the recipe in one step:

terminal
1curl -fsSL https://docker.recipes/api/recipes/go-fiber-postgres/run | bash

Troubleshooting

  • Connection refused to PostgreSQL: Ensure postgres service starts before app using depends_on and check DATABASE_URL format matches postgres://user:pass@host:port/db
  • Go Fiber panic on startup: Verify port 3000 is not in use and check Dockerfile EXPOSE directive matches application Listen port
  • Database migration errors: Use golang-migrate tool and ensure PostgreSQL extensions are installed before running schema changes
  • High memory usage: Configure PostgreSQL shared_buffers and Go's sql.DB SetMaxOpenConns/SetMaxIdleConns parameters appropriately
  • SSL connection errors: Add sslmode=disable to DATABASE_URL for development or configure proper SSL certificates for production
  • GORM connection timeout: Increase PostgreSQL max_connections setting and tune Go connection pool settings for concurrent load

Community Notes

Loading...
Loading notes...

Download Recipe Kit

Get all files in a ready-to-deploy package

Includes docker-compose.yml, .env template, README, and license

Ad Space