---
title: "Deploy Postgres HA pgvector"
description: "Vector search on a highly available Postgres cluster"
category: "Storage"
url: https://railway.com/deploy/deploy-postgres-ha-with-pgvector
---

# Deploy Postgres HA pgvector

Vector search on a highly available Postgres cluster

**[Deploy Postgres HA pgvector on Railway](https://railway.com/template/deploy-postgres-ha-with-pgvector)**

Machine-readable deploy manifest (JSON, validated by TemplateCI): https://railway.com/deploy/deploy-postgres-ha-with-pgvector/manifest.json

- **Creator:** onepush
- **Category:** Storage

## Template content

### pg-2

- **Image:** ghcr.io/onepush-dev/postgres-ha-node:18.6@sha256:31e91770e734468e2da836067da77c2d5505425c4e96779fee07c79f69f2f585

### pg-1

- **Image:** ghcr.io/onepush-dev/postgres-ha-node:18.6@sha256:31e91770e734468e2da836067da77c2d5505425c4e96779fee07c79f69f2f585

### Postgres

- **Image:** ghcr.io/onepush-dev/postgres-ha-router:18.6@sha256:7a01696b8161be78693b957469d63976a796c8d1e721f0c9aca48736743bce98
- **Health check:** /health

### pg-3

- **Image:** ghcr.io/onepush-dev/postgres-ha-node:18.6@sha256:31e91770e734468e2da836067da77c2d5505425c4e96779fee07c79f69f2f585

## Documentation

# Deploy and Host self hosted Postgres HA pgvector (Highly Available Postgres with pgvector for Vector Search) on Railway

This template deploys a 3-node Postgres 18.6 cluster managed by Patroni, with pgvector installed and a router that sends writes to the primary and similarity reads to replicas.

## About Hosting Postgres HA pgvector open-source software on Railway (self hosted pgvector on highly available Postgres template)

This template runs a 3-node Postgres 18.6 cluster managed by Patroni and etcd, with pgvector, PostGIS and pg_cron on every node.

Railway handles the containers, private networking and redeploy drain window. Your embeddings stay in ordinary Postgres tables you can query and back up like any other data.

## Why Deploy Postgres HA pgvector, the Pinecone alternative on Railway (Railway Free Trial)

Pinecone bills per index and query tier, and you can't control where embeddings live or how failover works. Railway's $5 trial is enough to test a real embedding workload.

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 pgvector 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 pgvector self hosting

Railway pre-wires this three-node Patroni cluster, while a VPS means building it yourself.

| Provider | What You Get with Railway | What You Get with the Other Provider |
| --- | --- | --- |
| DigitalOcean | Three Postgres nodes and the router from one template, with secrets wired. | Droplets where you install Patroni, etcd, and HAProxy by hand. |
| AWS | pg-1, pg-2, pg-3, and the router up together, with pgvector preinstalled. | EC2 and VPC setup plus your own cluster bootstrap and patching. |
| Hetzner | Pre-built node and router images with RW 5432 and RO 5433 routed. | Servers where you control everything but own each bootstrap step. |

A VPS gives you more hardware control, which suits teams already running Postgres in-house.

## Common Use Cases for hosted Postgres HA pgvector

- **RAG store that survives node loss:** Chunk embeddings sit in a vector column beside the source text. If the primary fails, Patroni promotes a replica and the router moves writes to it.
- **Semantic search for an app:** Product or help-center records are embedded once and queried with nearest-neighbour search on a vector(1536) column with an HNSW index.
- **Recommendation queries on replicas:** "More like this" reads go to port 5433 to spread load. Replicas are asynchronous by default, so a brand-new vector may take a moment to show up there.
- **Hybrid SQL plus vector filters:** A where clause on tenant or category combines with vector ordering in one query. Enable iterative index scans for selective filters, or the index may return fewer rows than you asked for.
- **Embeddings beside relational data:** Users, orders and their embeddings share one database, so joins, transactions and backups cover both.

## Dependencies for Postgres HA pgvector Docker hosted on Railway

The template runs as Railway services built from two official images, so you don't install Patroni, etcd, or HAProxy yourself.

### Deployment Dependencies for Managed Postgres HA pgvector Service (Database)

You get four services: pg-1, pg-2, and pg-3 for the Patroni-managed nodes, plus the Postgres router.

### Implementation Details for Postgres HA pgvector (Using Postgres HA official docker image)

Nodes read PATRONI_SCOPE (pg-ha), PGHA_NODES (the private domains of all three nodes), and PG_SHARED_BUFFERS (256MB by default).

## How does Postgres HA pgvector compare against other ways to host vector search on Postgres

### Postgres HA pgvector vs Pinecone (Pinecone Alternative)

- **Where data lives:** Pinecone is managed SaaS, so embeddings sit on its infrastructure. Postgres HA pgvector keeps vectors in your own tables beside relational data.
- **Joins and filters:** You can join vector results to users or permissions in plain SQL. Pinecone wins if you want zero database to operate.
- **Failover:** Patroni promotes a replica when the primary fails, and the vectors are already there.

### pgvector vs Qdrant (Qdrant Alternative)

- **Footprint:** pgvector is an extension inside Postgres, so there is one database to back up. Qdrant is a separate service to run alongside it.
- **Search features:** Qdrant has richer payload filtering and vector-specific tooling. pgvector gives you SQL, transactions, and foreign keys.
- **Scale:** Qdrant is built for very large collections. Past tens of millions of vectors, benchmark pgvector before committing.

### Postgres HA vs a single Postgres with pgvector (Single Node Alternative)

- **Availability:** A single node has no failover, so one failure takes search down. The HA cluster survives a node loss.
- **Read scaling:** Similarity queries on port 5433 spread load across replicas.
- **Simplicity:** A single node is cheaper and easier to reason about. If brief downtime is acceptable, it is the honest choice.

### pgvector vs Weaviate (Weaviate Alternative)

- **Architecture:** Weaviate is an open-source vector database with its own modules. pgvector adds vector types and indexes to Postgres you may already run.
- **Ecosystem:** Weaviate has built-in vectorizer modules and its own API. pgvector uses standard SQL and drivers your team knows.
- **Index tuning:** pgvector exposes HNSW and IVFFlat with settings like m, ef_construction, and ef_search. Weaviate manages indexing internally with less direct SQL control.

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

1. Connect to the router read-write port 5432 with psql using the template URL plus sslmode=require, since the certificate is self-signed.
2. Run CREATE EXTENSION IF NOT EXISTS vector on the primary. Replicas get it through replication.
3. Create a table with a vector column sized to your embedding model, such as 1536.
4. Insert your embeddings, raise maintenance_work_mem, then build an HNSW index with vector_cosine_ops.
5. Run a nearest-neighbour query ordered by cosine distance.
6. Send read-only search traffic to port 5433 and confirm pg_is_in_recovery returns true there.

```sql
-- Port 5432 (primary)
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE docs (
  id bigserial PRIMARY KEY,
  body text,
  embedding vector(1536)
);
SET maintenance_work_mem = '2GB';
CREATE INDEX docs_embedding_hnsw ON docs
  USING hnsw (embedding vector_cosine_ops)
  WITH (m = 16, ef_construction = 64);

-- Port 5433 (replica)
SELECT pg_is_in_recovery();  -- expect true
```

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

Run the same three-node Patroni cluster on your own VPS. You will need Docker on each host, a private network between them, and enough RAM for three Postgres nodes plus etcd.

### Clone the Repository

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

### Install Dependencies

```bash
sudo apt-get update
sudo apt-get install -y docker.io
docker --version
```

Open router ports 5432 (read-write), 5433 (read-only) and 8080 (health) only to networks that need them.

### Configure Environment Variables

```bash
# same values on every node: superuser, replication and REST API passwords
# plus each node's private hostname, so Patroni and etcd can find the peers
```

### Start the Postgres HA pgvector Application

```bash
# on each node host, then on the router host
docker pull ghcr.io/onepush-dev/postgres-ha-node:18.6
docker pull ghcr.io/onepush-dev/postgres-ha-router:18.6
curl -s http://localhost:8080/status
```

Railway handles the private networking, service wiring and secret generation for you, which this self-hosted path leaves to you.

## Official Pricing of Postgres HA pgvector (Postgres HA pricing)

Postgres HA pgvector is free and open source, so there is no license fee to pay. You pay only for the infrastructure the cluster runs on.

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

Running this cluster yourself or on Railway comes down to how much of the Patroni, etcd, and HAProxy plumbing you want to own.

### Monthly cost of self hosting Postgres HA pgvector on Railway

On Railway, expect roughly $25-60 per month for the three Postgres nodes plus the router at light use. Railway's $5 trial lets you test the cluster before you commit.

### System Requirements for Hosting Postgres HA pgvector on a VPS

If you self host on a VPS instead, plan for three nodes so Patroni can keep a quorum through a failover, plus enough RAM to hold your HNSW graphs in memory.

## Frequently Asked Questions (FAQs)

### Which pgvector version ships in this template?

The template runs Postgres 18.6 with pgvector included, so you do not install it by hand.

### Should I use HNSW or IVFFlat?

HNSW is the better default for most workloads, with a better speed and recall trade-off, though it builds more slowly and uses more memory.

### Can the read-only replicas serve vector search?

Yes, send similarity queries to port 5433 to spread load across the replicas. Replication is asynchronous by default, so a vector you just inserted may not show up there immediately.

### What happens to my vector data on failover?

Patroni promotes a replica automatically, and replicated data stays intact. Writes that had not yet reached a replica when the old primary failed can be lost, so plan for that gap.

### How much RAM do I need for the indexes?

Plan RAM around your index size plus shared_buffers, since HNSW graphs are best cached in memory.

### Does it support halfvec?

Yes, halfvec stores half-precision vectors and can be indexed for up to 4,000 dimensions.


## Similar templates

- [RustFS | (Just Updated) S3 Object Store, Public URL and Keys Set](https://railway.com/deploy/rustfs-or-just-updated-s3-object-store-p) — S3-compatible object store, keys and public URL preset, volume kept
- [TimescaleDB](https://railway.com/deploy/timescaledb-railway-template) — A PostgreSQL-based time-series database for analytics on Railway.
- [Garage S3 Storage](https://railway.com/deploy/garage-s3-storage) — Ultra-light S3 server: fast, open-source, plug-and-play.

Open this page in a browser: https://railway.com/deploy/deploy-postgres-ha-with-pgvector
