Vector database for RAG on Railway: pgvector

By Tracklayer · Updated 2026-11-20

Short answer: if your app already uses Postgres, start with pgvector. It adds vector similarity search to PostgreSQL, so embeddings sit next to the rest of your data and you query them with SQL, with HNSW or IVFFlat indexes. On Railway, a template can deploy PostgreSQL 18 with pgvector 0.8, PgBouncer connection pooling and daily backups in one project, with no separate vector service to run.

Deploy pgvector on Railway

When pgvector is a good fit for RAG

Retrieval-augmented generation (RAG) stores chunks of your documents as embeddings and retrieves the closest ones for each question. With pgvector you keep chunks, metadata and embeddings in one database. That lets you filter by user, tenant or date in the same SQL query as the similarity search, and you operate one database instead of two.

Typical uses:

Vector database templates people deploy on Railway

TemplateCreatorDeploymentsServices
pgvectorBrody's Projects2,0771
QdrantVamos Automatizar6731
ChromaDBISDev5112
pgvector-pg17ProCode3971
n8n 2 AI Agent Stack (Ollama + Qdrant)Tracklayer1210
Postgres 18 + pgvector Vector DatabaseTracklayer93
Mem0 v2 AI Memory LayerTracklayer64

Railway marketplace data as of 2026-11-19. Full ranking: Best vector database templates on Railway.

The table shows other vector databases and pgvector variants. Compare their services and descriptions on each template page before you choose. A separate vector database can make sense when you do not run Postgres at all, but then you operate another service and keep two stores in sync. If Postgres is already in your stack, one database is simpler to host, back up and secure.

Deploy pgvector on Railway, step by step

These steps use Tracklayer’s PostgreSQL 18 with pgvector template. It runs Railway’s own postgres-ssl:18 image, so ${{Postgres.*}} references work like they do with Railway’s Postgres.

  1. Deploy the template. It creates Postgres (with a volume), PgBouncer, a Postgres Backup cron job and a Railway Bucket.
  2. Copy the public URL. On the Postgres service, copy DATABASE_PUBLIC_URL. The password is generated.
  3. Connect. Run psql "{DATABASE_PUBLIC_URL}?sslmode=require". The certificate is self-signed.
  4. Check the extension. SELECT extversion FROM pg_extension WHERE extname = 'vector'; confirms pgvector is ready. The extension is created on first boot.
  5. Create a table and an index.
CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(1536));
CREATE INDEX ON items USING hnsw (embedding vector_cosine_ops);
  1. Create an app role. Use a dedicated role for your app instead of the postgres superuser.
  2. Connect your app from Railway. Reference ${{Postgres.DATABASE_POOL_URL}} for queries and ${{Postgres.DATABASE_URL}} for migrations and index builds.

Data lives on the Postgres volume, so it persists across redeploys. To keep the database private, remove the TCP proxy in Settings, Networking.

Pooling, tuning and large indexes

PgBouncer 1.26.1 runs in transaction mode on the private network. Use it for application queries, and use the direct URL for anything that needs a session. For queries through the pool, set hnsw.ef_search with SET LOCAL inside a transaction.

At every start, the template writes memory and parallel-worker settings from the container’s limits. If you resize the service, set PG_MEMORY_LIMIT_MB to match. For a large index build, run SET maintenance_work_mem = '2GB'; on the direct URL and keep it below three quarters of /dev/shm (1 GiB), or also set max_parallel_maintenance_workers = 0.

Backups and restore

A cron job runs daily at 03:15 UTC. It dumps roles and every database with pg_dump, verifies the dump, writes it to the Bucket and keeps 14 days (at least 3 runs). Run one now with railway cron run --service "Postgres Backup". To restore, download {database}.dump from the Bucket and run pg_restore with a PostgreSQL 18 client.

What it costs

Railway bills usage. The template’s figures are estimates, not measurements: about $1.50 per month idle and about $16 per month for a few GB of data with daily backups. Memory and volume size drive most of it. Hobby volumes start at 5 GB.

Limits

Other options

For related setups, see Postgres templates on Railway and the full ranking of vector database templates. If you use MongoDB instead, see MongoDB with a replica set.

FAQ

Do I need a separate vector database for RAG?

Not always. pgvector adds vector similarity search to PostgreSQL, so document chunks, metadata and embeddings can live in one database and be retrieved with SQL filters. A dedicated vector database is another option; the table above lists the vector templates people deploy on Railway.

How do I connect my app?

Use DATABASE_POOL_URL for application queries through PgBouncer, DATABASE_URL for migrations, CREATE INDEX, LISTEN/NOTIFY and session settings, and DATABASE_PUBLIC_URL from outside Railway with ?sslmode=require. Prisma needs ?pgbouncer=true on the pooled URL.

What drives the cost?

Postgres memory and its volume, which grows with data and indexes. HNSW index builds are fastest when the graph fits in maintenance_work_mem, so large indexes call for more RAM. Backups add Bucket storage and upload egress. The template estimates about $1.50 per month idle and about $16 for a few GB of data with daily backups; these are estimates.

How do I upgrade?

Postgres follows Railway's postgres-ssl:18 tag, with minor and security updates on redeploy. After a pgvector update, run ALTER EXTENSION vector UPDATE. PgBouncer is pinned to 1.26.1; change the tag to upgrade it.

Any licence notes?

PostgreSQL and pgvector use the PostgreSQL Licence, and PgBouncer uses the ISC licence. This is a community template, not affiliated with the PostgreSQL project, pgvector or Railway.