---
title: "Deploy PG Vector (PostgreSQL Vector Database)"
description: "PostgreSQL 18 with pgvector for embeddings and AI similarity search."
category: "Storage"
url: https://railway.com/deploy/pgvector-vector-database
---

# Deploy PG Vector (PostgreSQL Vector Database)

PostgreSQL 18 with pgvector for embeddings and AI similarity search.

**[Deploy PG Vector (PostgreSQL Vector Database) on Railway](https://railway.com/template/pgvector-vector-database)**

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

- **Creator:** blueorange
- **Category:** Storage

## Template content

### pgvector https://devicons.railway.app/i/postgresql.svg

- **Image:** pgvector/pgvector:pg18
- **Start command:** `/bin/sh -c "unset PGPORT; docker-entrypoint.sh postgres --port=5432"`

## Documentation

# Deploy and Host pgvector on Railway

![PostgreSQL elephant logo](https://www.postgresql.org/media/img/about/press/elephant.png)

pgvector adds vector similarity search to PostgreSQL, with **23,000+ GitHub stars**: store embeddings next to your regular data and query them with SQL. This template runs PostgreSQL 18 with pgvector on Railway, ready for RAG, semantic search and recommendations. Pinecone's Standard plan has a $50/month minimum — a pgvector database on Railway typically costs a few dollars a month.

---

## What This Template Deploys

| Service | Purpose |
|---|---|
| **pgvector** | PostgreSQL 18 with the pgvector extension (`pgvector/pgvector:pg18`), port 5432 on the private network plus a TCP proxy for external access. |
| **Persistent Volume** | Mounted at `/var/lib/postgresql` — tables, embeddings and indexes survive redeploys. |

A single database service with generated credentials. Use `DATABASE_URL_PRIVATE` from other Railway services and `DATABASE_URL` (via the TCP proxy) from outside Railway.

---

## About Hosting pgvector

A dedicated vector database means another vendor, another bill and syncing data between systems. pgvector keeps embeddings in Postgres, so filters, joins and transactions work on vectors and the rest of your data together.

On Railway, Postgres 18 with pgvector runs on a persistent volume with generated credentials, a private hostname for your apps and a public TCP proxy for tools like psql or your laptop.

**Enable the extension once.** Connect and run `CREATE EXTENSION IF NOT EXISTS vector;` in each database that needs it. Then add a `vector(1536)` (or your model's dimension) column and an HNSW index for fast search.

Typical cost: **~$2–8/month** on Railway for small to medium datasets, plus volume storage. Pinecone's Standard plan has a $50/month minimum, and Supabase Pro starts at $25/month.

---

## Deploy pgvector in Under 2 Minutes

1. Click **Deploy on Railway** — Postgres with pgvector starts on a volume (~1 minute).
2. Copy `DATABASE_URL` from the service variables and connect with `psql`.
3. Run `CREATE EXTENSION IF NOT EXISTS vector;`.
4. Create a table with a `vector` column and an HNSW index.
5. Point your app at `${{pgvector.DATABASE_URL_PRIVATE}}` and start storing embeddings.

No separate vector vendor. No sync jobs. No minimum spend.

---

## Common Use Cases

- **Pinecone alternative** — "postgres vector database" without a monthly minimum
- **RAG backends** — store document chunks and embeddings for LLM apps
- **Semantic search** — search products, docs or tickets by meaning
- **Recommendations** — nearest-neighbour queries on user or item vectors
- **AI agent memory** — long-term memory for LangChain, LlamaIndex or Langflow
- **Hybrid search** — combine SQL filters, full-text search and vector similarity

---

## Configuration

| Variable | Required | Description |
|---|---|---|
| `POSTGRES_PASSWORD` | 🔐 Auto-generated | Database password. |
| `DATABASE_URL_PRIVATE` | ✅ Auto-injected | Private connection URL — use it from other Railway services. |
| `DATABASE_URL` | ✅ Auto-injected | Public connection URL through the TCP proxy, for tools outside Railway. |
| `PGHOST`, `PGPORT`, `PGUSER`, `PGDATABASE` | ✅ Auto-injected | Individual connection settings. |
| `POSTGRES_DB`, `POSTGRES_USER` | Optional | `railway` and `postgres` by default. |

**Use the private URL from your apps.** `DATABASE_URL` goes through the public TCP proxy, which adds latency and egress costs; services in the same project should use `DATABASE_URL_PRIVATE`.

---

## pgvector on Railway vs. Alternatives

| | pgvector on Railway | Pinecone | Supabase | Qdrant (self-hosted) |
|---|---|---|---|---|
| **Monthly cost** | ~$2–8 usage | Free Starter, $50 minimum on Standard | Free (pauses when idle), Pro from $25 | Free, you host it |
| **Self-hosted** | ✅ Yes | ❌ No | ⚠️ Possible, complex | ✅ Yes |
| **SQL joins with your app data** | ✅ Yes | ❌ No | ✅ Yes (Postgres) | ❌ No |
| **Free tier limits** | ✅ None beyond usage | ⚠️ 2 GB, 5 indexes | ⚠️ 500 MB, pauses after a week | ✅ None |
| **Purpose-built vector features** | ⚠️ HNSW and IVFFlat indexes | ✅ Yes | ⚠️ pgvector | ✅ Yes |
| **License** | ✅ PostgreSQL License | ❌ Proprietary | ✅ Apache-2.0 (platform) | ✅ Apache-2.0 |

---

## Dependencies for pgvector Hosting

- Railway account — Hobby ($5/month) covers small datasets; volume storage is billed by size.
- An embedding model (OpenAI, Cohere, a local model) to create the vectors.

### Deployment Dependencies

- [pgvector GitHub](https://github.com/pgvector/pgvector) — extension docs and index options
- [PostgreSQL Docs](https://www.postgresql.org/docs/) — database reference
- [pgvector Docker image](https://hub.docker.com/r/pgvector/pgvector) — the image this template runs
- [Railway Volumes Documentation](https://docs.railway.com/volumes) — persistence

### Implementation Details

The service runs `pgvector/pgvector:pg18`, starting Postgres on port 5432 with data in `/var/lib/postgresql/data/pgdata` on the volume. Railway exposes it on the private network and through a TCP proxy for external clients.

After deploy, enable the extension with `CREATE EXTENSION vector;`, store embeddings in `vector(n)` columns, and add an HNSW index (`USING hnsw (embedding vector_cosine_ops)`) once you have data, so similarity queries stay fast.

---

## Frequently Asked Questions

**What is pgvector?**
An open-source PostgreSQL extension that adds a vector data type and similarity search, so you can store and query embeddings with SQL.

**How much does pgvector on Railway cost compared to Pinecone?**
Typically $2–8/month for small to medium datasets. Pinecone's Standard plan has a $50/month minimum, and Supabase Pro starts at $25/month.

**Why do I get "type vector does not exist"?**
The extension isn't enabled in that database yet. Run `CREATE EXTENSION IF NOT EXISTS vector;` and try again.

**Do I lose data when I redeploy?**
No. All tables, embeddings and indexes are stored on the persistent volume at `/var/lib/postgresql`.

**Can I use pgvector with LangChain or LlamaIndex?**
Yes. Both have pgvector integrations — pass them the connection URL and they create and query the tables for you.

**How many vectors can it handle?**
Millions on a modest instance with an HNSW index; performance depends on dimensions, RAM and index settings. Scale the service's resources in Railway as your data grows.

---

## Why Deploy and Host pgvector 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 pgvector on Railway, you get PostgreSQL 18 with vector search on a persistent volume for a few dollars a month, right next to your AI apps.


## 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/pgvector-vector-database
