Railway

Deploy HTMX + Fiber + Go - Starter

HTMX + Fiber + Go + PostgreSQL starter with one-click deploy

Deploy HTMX + Fiber + Go - Starter

/var/lib/postgresql/data

Deploy and Host HTMX + Go + PostgreSQL Starter on Railway

HTMX + Go + PostgreSQL Starter is a production-ready template for building hypermedia-driven web applications. It combines HTMX for dynamic interactions without JavaScript, Go with Fiber and Templ for type-safe server-side rendering, and PostgreSQL for persistent storage. Deploy in one click and start building.

About Hosting HTMX + Go + PostgreSQL Starter

The template deploys as a single Go binary built via multi-stage Dockerfile (~15MB final image, ~31s build time). It connects to a PostgreSQL instance over Railway's private network using the pgx driver with connection pooling. Database migrations run automatically on startup, creating the required tables without manual SQL. The application reads DATABASE_URL from environment variables and listens on the port assigned by Railway. A health check endpoint at /health pings the database and returns status, ensuring Railway can verify deployments before routing traffic. Tailwind CSS and HTMX load via CDN, so there is no frontend build step.

Common Use Cases

  • Rapid prototyping of server-rendered web apps that need dynamic interactions without the complexity of React, Vue, or other JavaScript frameworks
  • Building internal tools, admin panels, and CRUD dashboards where Go's performance and type safety matter but a full SPA is overkill
  • Learning HTMX patterns (hx-get, hx-post, hx-swap, hx-target) with a working reference application backed by a real database

Dependencies for Hosting

  • A Railway PostgreSQL database instance (added via the Railway dashboard or CLI)
  • The DATABASE_URL environment variable set to ${{Postgres.DATABASE_URL}} on the web service

Deployment Dependencies

Why Deploy on Railway?

Railway is a singular platform to deploy your infrastructure stack. Railway will host your infrastructure so you don't have to deal with configuration, while allowing you to vertically and horizontally scale it.

By deploying HTMX + Go + PostgreSQL Starter on Railway, you are one step closer to supporting a complete full-stack application with minimal burden. Host your servers, databases, AI agents, and more on Railway.


What's Inside

A complete, working Todo application that demonstrates the full HTMX + Go stack. No JavaScript frameworks, no build steps for the frontend, no client-side state management. Just HTML over the wire.

LayerTechnologyRole
FrontendHTMX 2.0.7 + Tailwind CSSHypermedia-driven interactions via CDN
TemplatingTempl v0.3Type-safe Go templates with IDE support
RoutingFiber v2.52High-performance HTTP framework built on fasthttp
DatabasePostgreSQL + pgx v5.9Connection pooling, prepared statements
DeployDockerfile (multi-stage)31s build, ~15MB final image

Why This Stack

HTMX lets you build dynamic UIs by returning HTML fragments from the server instead of JSON. No React, no Vue, no bundle. Your Go backend renders HTML, HTMX swaps it into the DOM. The browser does what browsers do best.

Templ gives you type-safe, composable HTML components written in Go. Compile-time checking catches broken templates before they reach production. IDE autocompletion works because components are just Go functions.

Fiber is an Express-inspired Go web framework built on fasthttp, one of the fastest HTTP engines for Go. It provides an intuitive API, built-in middleware, and zero-allocation routing.

PostgreSQL needs no introduction. The pgx driver gives you connection pooling, prepared statements, and direct access to PostgreSQL-specific features without an ORM in the way.


Project Structure

.
├── cmd/server/
│   └── main.go              # Server setup, routes, middleware
├── internal/
│   ├── database/
│   │   └── database.go      # pgx connection pool + auto-migration
│   ├── handlers/
│   │   └── handlers.go      # HTTP handlers (CRUD + health check)
│   └── models/
│       └── models.go        # Data structures
├── templates/
│   ├── layout.templ          # Base HTML with HTMX + Tailwind CDN
│   ├── home.templ            # Home page composition
│   └── components.templ      # HTMX-powered interactive components
├── Dockerfile                # Multi-stage build (golang -> alpine)
└── go.mod

HTMX Patterns Demonstrated

The Todo app showcases core HTMX patterns you'll use in real projects:

  • hx-post + hx-target + hx-swap="afterbegin" - Create items and prepend to list without page reload
  • hx-patch + hx-target (self) - Toggle completion with in-place swap
  • hx-delete + hx-swap="outerHTML" - Remove items with smooth DOM removal
  • hx-on::after-request - Reset form after successful submission
  • Health check endpoint - /health returns JSON status with DB ping

Deploy to Railway

Click the button above or:

  1. Fork this repo
  2. Create a new project on Railway
  3. Add a PostgreSQL database
  4. Add a service from your forked repo
  5. Set the variable: DATABASE_URL = ${{Postgres.DATABASE_URL}}
  6. Railway auto-detects the Dockerfile and deploys

The app auto-migrates the database on startup. No manual SQL needed.


Local Development

# Prerequisites: Go 1.25+, PostgreSQL running locally

# Install templ CLI
go install github.com/a-h/templ/cmd/templ@latest

# Clone and run
git clone https://github.com/atoolz/railway-htmx-go-templ-fiber-pg.git
cd railway-htmx-go-templ-fiber-pg

# Set your database URL
export DATABASE_URL="postgres://user:pass@localhost:5432/mydb?sslmode=disable"

# Generate templ files and run
templ generate
go run ./cmd/server

Open http://localhost:8080.


Environment Variables

VariableRequiredDefaultDescription
DATABASE_URLYes-PostgreSQL connection string
PORTNo8080Server port (Railway sets this automatically)

Part of the HTMX Railway Collection

This is one of 15 HTMX starter templates covering different backend stacks, all following the same pattern and ready for Railway deployment:

StackStatus
Go + EchoAvailable
Go + FiberThis repo
Go + ChiComing soon
Python + FastAPIComing soon
Python + DjangoComing soon
Java + Spring Boot (PG)Coming soon
Java + Spring Boot (MySQL)Coming soon
PHP + LaravelComing soon
Node + ExpressComing soon
Ruby + Rails 8Coming soon
.NET + RazorComing soon
Rust + AxumComing soon
Node + HonoComing soon
Bun + ElysiaComing soon
Elixir + PhoenixComing soon

License

MIT


Built by AToolZ for the HTMX community


Template Content

More templates in this category

View Template
Foundry Virtual Tabletop
A Self-Hosted & Modern Roleplaying Platform

Lucas
View Template
(v1) Simple Medusa Backend
Deploy an ecommerce backend and admin using Medusa

Shahed Nasser
View Template
peppermint
Docker-compose port for peppermint.sh

HamiltonAI