• Stars
    star
    287
  • Rank 138,895 (Top 3 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created over 3 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Postgres + Stolon for HA clusters as Fly apps.

High Availability Postgres on Fly.io

This repo contains all the code and configuration necessary to run a highly available Postgres cluster in a Fly.io organization's private network. This source is packaged into Docker images which allow you to track and upgrade versions cleanly as new features are added.

If you just want to get a standard Postgres standalone or highly-available setup on Fly, check out the docs.

Customizing cluster behavior

Fly Postgres clusters are just regular Fly applications. If you need to customize Postgres in any way, you may fork this repo and deploy using normal Fly deployment procedures. You won't be able to use fly postgres commands with custom clusters. But it's a great way to experiment and potentially contribute back useful features!

Follow the rest of this README to run a customized setup.

Prepare a new application

You'll need a fresh Fly application in your preferred region to get started. Run these commands within the fork of this repository.

fly launch --no-deploy

This gets you started with a Fly application and an associated config file. Choose yes when asked whether to copy the existing configuration to the newly generated app.

Set secrets

This app requires a few secret environment variables. Generate a secure string for each, and save them.

SU_PASSWORD is the PostgreSQL super user password. The username is flypgadmin. Use these credentials to run high privilege administration tasks.

REPL_PASSWORD is used to replicate between instances.

OPERATOR_PASSWORD is the password for the standard user postgres. Use these credentials for connecting from your application.

fly secrets set SU_PASSWORD=<PASSWORD> REPL_PASSWORD=<PASSWORD> OPERATOR_PASSWORD=<PASSWORD>

Set the PRIMARY_REGION environment variable within your fly.toml

The PRIMARY_REGION value lets Stolon know which Postgres instances are eligible for election in the event of a failover. If this value is not set to the correct region, your cluster may not boot properly.

Deploy one instance

First, get one instance deployed in your preferred start region.

  1. fly volumes create pg_data --region ord --size 10
  2. fly deploy
  3. fly status

Add a replica

Scaling up will automatically setup a replica for you. Do that now in the same region.

  1. fly volumes create pg_data --region ord --size 10
  2. fly scale count 2
  3. fly status

Add a replica in another region

Scale to another region by creating a volume there. Now you should have a primary/replica pair in ord and a replica in syd.

  1. fly volumes create pg_data --region syd --size 10
  2. fly scale count 3
  3. fly status

Connecting

Fly apps within the same organization can connect to your Postgres using the following URI:

postgres://postgres:<operator_password>@<postgres-app-name>.internal:5432/<database-name>

Connecting to Postgres from your local machine

  1. Forward the server port to your local system with flyctl proxy:
flyctl proxy 5432 -a <postgres-app-name>
  1. Postgres needs to be installed on your local machine.

  2. Use psql to connect to your Postgres instance on the forwarded port.

psql postgres://postgres:<operator_password>@localhost:5432

Enabling TimescaleDB

This app includes the TimescaleDB extension. To enable TimescaleDB, take the following steps:

  1. Ensure your Postgres app is running >= v0.0.28.
# View your image details
fly image show --app <app-name>

# Update to the latest ( Nomad-based apps )
fly image update --app <app-name>
  1. Configure Postgres to preload the TimescaleDB library
fly pg config update --shared-preload-libraries timescaledb --app <app-name>
  1. Restart Postgres
fly postgres restart --app <app-name>
  1. Create the extension
# Connect to your target database
fly pg connect --app <app-name> --database <db-name>

# Create the extension
CREATE EXTENSION IF NOT EXISTS timescaledb;

Having trouble?

Create an issue or ask a question here: https://community.fly.io/

Contributing

If you're looking to get involved, fork the project and send pull requests.

More Repositories

1

live_beats

Elixir
1,074
star
2

dockerfile-rails

Provides a Rails generator to produce Dockerfiles and related files.
Dockerfile
422
star
3

tictac

Demonstration of building a clustered, distributed, multi-player, turn-based game server written in Elixir.
Elixir
339
star
4

litefs-js

JavaScript utilities for working with LiteFS on Fly.io
TypeScript
154
star
5

nginx-cluster

A horizontally scalable NGINX caching cluster
Shell
125
star
6

terraform-provider-fly

Terraform provider for the Fly.io API
Go
114
star
7

dockerfile-node

Dockerfile generator for Node.js
JavaScript
111
star
8

edge-apollo-cache

Run and cache results from your Apollo GraphQL server on the edge with Fly
JavaScript
91
star
9

redis-geo-cache

A global Redis cache
Shell
81
star
10

bun

Bun JS app doing basically nothing
TypeScript
76
star
11

redis

Launch a Redis server on Fly
Shell
70
star
12

fly-run-this-function-on-another-machine

This is a simple example on how to spawn a Fly.io machine and run a function from there.
JavaScript
63
star
13

hello-rust

Rust example app on Fly.io
Dockerfile
62
star
14

nats-cluster

Global messaging for apps that need to talk to each other.
Go
44
star
15

postgres-flex

Postgres HA setup using repmgr
Go
44
star
16

tailscale-router

Go
40
star
17

rds-connector

Trivial Terraform example for a WireGuard peer to RDS
HCL
38
star
18

docker-daemon

A Docker daemon to run in Fly and access via a WireGuard peer.
Shell
38
star
19

fly-laravel

Run your Laravel apps on Fly
PHP
38
star
20

hello_elixir

An example for building and deploying an Elixir application to Fly using a Dockerfile
Elixir
38
star
21

litestream-base

A base Docker image for adding Litestream to apps
Dockerfile
33
star
22

smokescreen

An example of deploying Smokescreen on Fly.io
Go
31
star
23

go-example

A minimal Go application for tutorials
Go
29
star
24

python-hellofly-flask

A Pythonic version of the Hellofly example
Python
29
star
25

laravel-docker

Base Docker images for use with Laravel on Fly.io
Shell
28
star
26

cockroachdb

Shell
27
star
27

nginx

A fly app nginx config
Dockerfile
23
star
28

supercronic

Run periodic jobs on Fly with supercronic
Dockerfile
21
star
29

hello-fly-langchain

A minimal example of how to deploy LangChain to Fly.io using Flask
Python
21
star
30

hellonode-builtin

A minimal Fly example Node application for use in tutorials
JavaScript
20
star
31

vscode-remote

Shell
19
star
32

privatenet

Examples around querying 6PN private networking on Fly
JavaScript
18
star
33

node-demo

Fly.io Node.js demo
JavaScript
18
star
34

puppeteer-js-renderer

A service to render js for web scraping hosted on fly.io
JavaScript
17
star
35

hello-static

Create a static website with Fly - HTML from the example
HTML
16
star
36

ghost-litestream

Ghost + Litestream for global sqlite blogging
Dockerfile
16
star
37

wordpress-sqlite

Wordpress on SQLite
PHP
16
star
38

thumbnail_generator

Elixir
15
star
39

coredns

Authoritative CoreDNS on Fly.io
DIGITAL Command Language
15
star
40

ichabod

serf + headless chromium && CDP
Dockerfile
14
star
41

nix-base

Nix overlays for supporting Nix deployments on Fly.io
Nix
14
star
42

fly-log-local

Store Fly app logs locally.
Dockerfile
13
star
43

terraformed-machines

Example of Fly.io machines orchestration with Terraform and DNSimple
HCL
13
star
44

elixir_opentel_and_grafana

Project that goes with a Fly.io Phoenix Files article
Elixir
13
star
45

dockerfile-laravel

PHP
13
star
46

fastify-functions

Example Fastify server
JavaScript
12
star
47

postgres-migrator

Fly app that works to streamline Postgres migrations.
Dockerfile
12
star
48

pdf-appliance

Auto start machines that will generate PDFs for your application
TypeScript
12
star
49

hellodeno

A version of the Hellodeno example that uses flyctl's builtin deno builder
TypeScript
11
star
50

whisper-example

Fly GPU Machines transcribing an mp3 file with Whisper
Dockerfile
11
star
51

hello_elixir_sqlite

An example for building and deploying an Elixir application to Fly using a Dockerfile and SQLite!
Elixir
10
star
52

keydb

KeyDB server on Fly
Shell
9
star
53

fly-app-with-multiple-internal-ports

Example of how to deploy an app that has multiple ports listened to
JavaScript
9
star
54

fly-varnish

Dockerfile
9
star
55

grafana

Run Grafana on Fly
8
star
56

ollama-demo

@jmorganca's ollama.ai demo app on Fly.io
8
star
57

postgres-importer

Shell
8
star
58

nodejs-planetscale-read-replicas

A sample Node.js app that uses a Planetscale database with additional read-only regions
JavaScript
7
star
59

kong-api-gateway

Dockerfile
7
star
60

hostnamesapi

JavaScript examples for working with the new hostnames API on Fly
JavaScript
7
star
61

appkata-minio

MinIO S3-compatible storage on Fly
Dockerfile
7
star
62

autoscale-to-zero-demo

TypeScript
7
star
63

live-elements-demo

Live Elements Demo
Ruby
6
star
64

6pn-demo-chat

A Node-based websockets and NATS chat app which uses Fly 6PN networking
JavaScript
6
star
65

rqlite

Shell
6
star
66

deno-apollo

TypeScript
6
star
67

hello-fly

JavaScript
6
star
68

ssh-app

Run an SSH server to connect your privately networked Database apps to things like BI tools
Dockerfile
6
star
69

global-apollo-server

Fly global deployment with Apollo Server and Prisma
TypeScript
6
star
70

rails-nix

Deploy Rails apps on Fly.io with Nix
Ruby
5
star
71

code-server-dev-environment

Dockerfile
5
star
72

hello-flask

Example project demonstrating how to deploy a Flask app to Fly.io.
HTML
5
star
73

postgres

Deploy a Postgres database on Fly, ready for your Fly apps.
4
star
74

appkata-mqtt

An MQTT server app (with Mosquitto) with TLS+passwords
Dockerfile
4
star
75

global-rails

Ruby
4
star
76

globe-request-mapper

Elixir
4
star
77

buildkite-agent

Run a Buildkite agent on Fly with disk caching
Dockerfile
4
star
78

openresty-basic

Dockerfile
4
star
79

flydictionary

A light crud example for database examples
JavaScript
4
star
80

grpc-service

Running gRPC services on Fly.io
JavaScript
4
star
81

elixir_prom_ex_example

Elixir
4
star
82

hello-create-react-app

JavaScript
4
star
83

hello-remix

Sample Remix app setup for deployment on Fly.io
JavaScript
3
star
84

tcp-echo

TCP echo service for testing things that TCP
Go
3
star
85

rails-on-fly

Ruby
3
star
86

laravel-worker

Auto-scaled Laravel queue workers on Fly.io
PHP
3
star
87

pi-hole

Dockerfile
3
star
88

fly-nestjs

Example NestJS application configured for deployment on Fly.io
TypeScript
3
star
89

view-component-playground

Various view components tried in Rails
Ruby
3
star
90

python_gpu_example

A setup with Jupyter for GPU-enabled ML tinkering
Shell
3
star
91

hello-django

Example project demonstrating how to deploy a Django app to Fly.io.
Python
3
star
92

localai-demo

LocalAI demo app on Fly.io
Shell
3
star
93

rails-statics

Rails application to test the performance of Rails static assets
Ruby
3
star
94

hello-django-postgres

Python
2
star
95

fly-laravel-litefs

Guide on deploying multiple instances of a Laravel Fly app and integrating LiteFS and fly-replay to allow syncing SQLite database across the instances.
PHP
2
star
96

ollama-webui-demo

Shell
2
star
97

udp-echo-

Sample TCP/UDP Echo Service
Go
2
star
98

replicache-websocket

TypeScript
2
star
99

rails-machine-workers

A demonstration of how to use Fly Machines for "scale-to-0" ActiveJob background workers
Ruby
2
star
100

flygreeting

An example app for other examples to use.
Go
2
star