Deploy Postgres HA PostGIS

Spatial queries on a highly available Postgres cluster

Deploy Postgres HA PostGIS

/var/lib/postgresql/data

/var/lib/postgresql/data

/var/lib/postgresql/data

Deploy and Host self hosted Postgres HA PostGIS (Highly Available Postgres with PostGIS) on Railway

This template deploys a three node Patroni cluster behind a Postgres router, with PostGIS ready to enable on the primary. You get automatic failover and a read-only port for map reads.

About Hosting Postgres HA PostGIS open-source software on Railway (self hosted Postgres HA with PostGIS template)

Your data lives in your own Postgres cluster, including the spatial tables you build with PostGIS.

Why Deploy Postgres HA PostGIS, the Amazon RDS PostGIS alternative on Railway (Railway Free Trial)

Amazon RDS offers PostGIS as a managed extension, which suits teams already on AWS. Compare the real bill against your data size, since RAM and storage drive cost.

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 Postgres HA PostGIS 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.

Railway vs Other Hosting Providers and VPS for Postgres HA PostGIS self hosting

Here is how Railway compares when you want a three node Patroni cluster with PostGIS.

ProviderWhat You Get with RailwayWhat You Get with the Other Provider
DigitalOceanPg-1, pg-2, pg-3 and the router deploy from one template, with PostGIS ready to enable on port 5432.You provision droplets and install Patroni, etcd, and HAProxy yourself.
AWSThe cluster, private networking, and read-only port 5433 come pre-wired with no VPC setup.Self-managed Patroni on EC2 means you own the VPC, security groups, and failover tuning.
HetznerEverything lives in one project, so you skip writing systemd units and etcd configs.Cheap raw servers, but the Patroni and router layer is all on you.

Pick the option that matches how much infrastructure you want to own.

Common Use Cases for hosted Postgres HA PostGIS

  • Nearest-store lookups: Store points as geography(Point, 4326), index them with GiST, and filter with ST_DWithin so the planner can use the index.
  • Delivery zones and geofencing: Test whether a driver's latest GPS point falls inside a zone polygon. The index narrows candidates first, then PostGIS runs the exact containment check.
  • Map tile reads on the replica port: Send read-heavy tile queries to port 5433 (Postgres.DATABASE_URL_READONLY) and keep writes on 5432. Check replica lag against your tile freshness needs.
  • GPS tracking tables: Position pings are queried by area and time window. Run VACUUM ANALYZE after large batch inserts so the planner's statistics stay accurate.
  • Spatial joins for analytics: Joining orders to delivery zones gives distances in meters with geography. Expect more CPU than planar math, so keep the scope tight.

Dependencies for Postgres HA PostGIS Docker hosted on Railway

This template runs as a small cluster rather than a single container. Three Postgres nodes (pg-1, pg-2, pg-3) run Patroni and etcd, with a Postgres router in front of them.

Deployment Dependencies for Managed Postgres HA PostGIS Service (Database)

You need a Railway project with four services: pg-1, pg-2, pg-3, and the Postgres router. Each node also needs a volume mounted for its data directory.

Implementation Details for Postgres HA PostGIS (Using Postgres HA official docker image)

Leave the password variables alone on pg-2 and pg-3, because they reference pg-1 so all nodes share one superuser password. Generate the secret on pg-1 with ${{secret(32)}}.

How does Postgres HA PostGIS compare against other managed PostGIS hosting options

Each managed PostGIS option trades control, cost, and operational effort differently. Here is where this template fits and where the others do better.

Postgres HA PostGIS vs Amazon RDS PostGIS

  • Failover model: Three Patroni-managed nodes behind a router fail over automatically inside a cluster you control. RDS handles failover for you.
  • Read traffic: Map tile reads can go to port 5433 through the router. RDS has more mature replica tooling if you need that depth.
  • Extension setup: You run CREATE EXTENSION postgis; on the primary yourself, then verify with SELECT PostGIS_Full_Version();.

Postgres HA PostGIS vs Supabase PostGIS

  • Scope: This is a database cluster only. Supabase bundles auth, storage, and APIs if you want a full backend in one place.
  • Control: You get raw Postgres access and inspectable Patroni behavior. Supabase abstracts more of that away.
  • Spatial tuning: You own the GiST index strategy and ANALYZE cadence after bulk loads.

Postgres HA PostGIS vs Crunchy Bridge

  • Operations: Crunchy Bridge is a managed service with a long record running PostGIS, so it is the safer pick if you want a vendor to own day-to-day operations.
  • Portability: This template runs on Railway and can move to another VPS with the same Patroni stack.
  • Upgrades: Newer PostGIS builds need SELECT postgis_extensions_upgrade(); in each spatial database. The upgrade path inside the node image is not documented in the supplied material, so verify it first.

Postgres HA PostGIS vs Neon PostGIS

  • Architecture: Neon is serverless and scales compute separately from storage. This fixed three-node cluster suits steady workloads.
  • Latency: A local cluster behind a router keeps interactive map queries predictable. Serverless models can add cold-start effects.
  • Failover: Patroni fails over across the nodes you deploy. Neon manages failover for you, which is simpler if you do not want to see the cluster.

How to use Postgres HA PostGIS (the OSS database cluster)?

  1. Connect through the router on port 5432 with Postgres.DATABASE_PUBLIC_URL and sslmode=require. Skip verify-full, since the self-signed certificate fails it.
  2. Enable PostGIS as a superuser and confirm it with SELECT PostGIS_Full_Version();.
  3. Create a geography(Point, 4326) table, add a GiST index, insert points, and run an ST_DWithin radius query.
  4. Send read-heavy map tile queries to port 5433 with Postgres.DATABASE_URL_READONLY, and keep writes on the primary.
CREATE EXTENSION postgis;

CREATE TABLE stores (
  id serial PRIMARY KEY,
  loc geography(Point, 4326)
);

CREATE INDEX stores_loc_gix ON stores USING GIST (loc);

INSERT INTO stores (loc) VALUES
  (ST_SetSRID(ST_MakePoint(-118.2437, 34.0522), 4326)::geography);

SELECT id FROM stores
WHERE ST_DWithin(loc, ST_SetSRID(ST_MakePoint(-118.25, 34.05), 4326)::geography, 5000);

How to self host Postgres HA PostGIS on other VPS Services (Postgres HA self hosting guide)

This guide assumes a VPS with Docker, Docker Compose, and a domain you control.

Clone the Repository

Pull the template files onto your server.

git clone https://github.com/onepush-dev/postgres-ha.git
cd postgres-ha

Install Dependencies

Install Docker and the Compose plugin, then confirm they work.

sudo apt-get update && sudo apt-get install -y docker.io docker-compose-plugin
docker compose version

Configure Environment Variables

Copy the example env file and set real passwords for the superuser, replication, and REST API accounts.

cp .env.example .env
# set PATRONI_SUPERUSER_PASSWORD, PATRONI_REPLICATION_PASSWORD, PATRONI_RESTAPI_PASSWORD
# set PATRONI_SCOPE=pg-ha and PGHA_NODES to the three node addresses

Start the Postgres HA PostGIS Application

Bring up the nodes and router, then check that the cluster reports healthy.

docker compose up -d
curl -s http://your-domain:8080/status

Once the primary and both replicas show up, connect as postgres and run CREATE EXTENSION postgis; before loading spatial data.

Official Pricing of Postgres HA PostGIS (Postgres HA pricing)

Postgres HA PostGIS is free and open source, so there is no license fee to pay.

Postgres HA PostGIS cloud vs self hosted comparison (Pricing, features, costs, and more)

Running PostGIS on a three node Patroni cluster costs differently depending on where you host it. Here is how the two paths compare on cost and on the ops work you take on yourself.

Monthly cost of self hosting Postgres HA PostGIS on Railway

On Railway you pay for the three Postgres nodes plus the router. The $5 trial covers a first deploy and the smoke test in the README.

System Requirements for Hosting Postgres HA PostGIS on a VPS

On your own VPS, plan for three separate machines so failover means something, plus enough RAM to keep GiST indexes warm.

Frequently Asked Questions (FAQs)

Is PostGIS installed on every node?

Yes, the binaries ship in the node image on pg-1, pg-2, and pg-3. You still run CREATE EXTENSION postgis; once per database, through the router on the primary.

When should I use geography instead of geometry?

Use geography for data spread over large areas, since it uses spherical math and returns meters. Use geometry for compact areas with a sensible projection, because it is planar.

Which index type should I use?

Use a GiST index created with USING GIST on any geography or geometry column you filter on. GiST stores bounding boxes, so queries run an index filter first and then the exact test.

Does replication carry spatial data?

Yes, the replicas are standard Patroni streaming replicas, so spatial tables replicate like any other table.

How much lag should I expect on read replicas?

The docs give no lag figure, so measure it under your own load before pointing map tile queries at port 5433.

How do PostGIS upgrades work?

After installing a newer build, run SELECT postgis_extensions_upgrade(); in each spatial database, then confirm with SELECT postgis_full_version();.


Template Content

More templates in this category

View Template
RustFS | (Just Updated) S3 Object Store, Public URL and Keys Set
S3-compatible object store, keys and public URL preset, volume kept

SuperSlowSloth
0
View Template
TimescaleDB
A PostgreSQL-based time-series database for analytics on Railway.

Rakit
1
View Template
Garage S3 Storage
Ultra-light S3 server: fast, open-source, plug-and-play.

PROJETOS
8