
Deploy Khoj — Self-Hosted AI Second Brain
Self-host Khoj — chat with your docs, AI second brain
Just deployed
/var/lib/postgresql/data
Just deployed
Terrarium
Just deployed
SearxNG
Just deployed
Deploy and Host Khoj on Railway
Khoj is an open-source personal AI assistant and "second brain" — chat with your documents, run semantic search across your notes, browse the web, and build custom agents, all self-hosted so your knowledge stays private. Backed by Y Combinator with 34k+ GitHub stars, it connects to OpenAI, Anthropic, or Gemini and turns your files into a searchable, conversational knowledge base. This template deploys Khoj's full stack — server, pgvector database, code sandbox, and web search — pre-wired, so you have a private AI assistant over your own data in minutes.
What This Template Deploys
| Service | Purpose |
|---|---|
| Khoj | The assistant server, chat UI, and document engine (Django + Next.js) |
| PostgreSQL + pgvector | Vector embeddings of your documents for semantic search |
| Terrarium | Sandboxed Python execution for agents (data analysis, code) |
| SearxNG | Privacy-focused metasearch for the assistant's web research |
All four connect over Railway's private network, pre-wired so the assistant, its memory, code execution, and web search work together out of the box.
About Hosting
Khoj is a capable multi-service assistant, and a few specifics make it deploy cleanly on Railway — all handled here.
Four services, pre-wired — the setup that's hard to do by hand. Khoj isn't a single container: it needs the server, a pgvector database, the Terrarium code-execution sandbox, and SearxNG for web search, all connected. This template wires them over the private network so document search, agent code execution, and web research work immediately, instead of assembling four services and their URLs manually.
pgvector, not standard Postgres. Khoj stores document embeddings and runs semantic search through the pgvector extension, which Railway's managed Postgres doesn't include — so this template uses the pgvector/pgvector:pg16 image. This is what lets Khoj retrieve the right context from your notes by meaning, not just keywords.
Reduce GUNICORN_WORKERS to fit Railway's RAM. This is the key Railway-specific tuning: Khoj defaults to 6 Gunicorn workers, which exceeds an 8 GB plan and causes out-of-memory crashes. Set GUNICORN_WORKERS to 2–3 so the server stays within the plan limit. With cloud LLM APIs, Khoj then runs comfortably in about 4 GB. This template sets a sensible worker count for Railway.
Use cloud LLM APIs — not local models. Khoj supports local models via Ollama, but that needs a GPU and 8–16 GB RAM, which isn't practical on Railway. Use cloud APIs instead: set OPENAI_API_KEY, ANTHROPIC_API_KEY, or GEMINI_API_KEY (or add them in the admin panel), and you bring your own keys, paying each provider directly.
Set your admin and Django secret. KHOJ_ADMIN_EMAIL and KHOJ_ADMIN_PASSWORD create your admin account, and KHOJ_DJANGO_SECRET_KEY (a long random 50+ character string) secures sessions — keep it stable. After deploy, sign in at /server/admin to add API keys and configure chat models.
Your data persists in Postgres. Documents, embeddings, chat history, and agent configurations live in the pgvector database, so they survive redeploys — back it up to preserve your second brain.
Typical cost: ~$10–20/month on Railway for the four services, plus your LLM provider usage. Khoj is AGPL-3.0 and free.
How It Compares
| Khoj (self-hosted) | ChatGPT | NotebookLM | Cloud AI assistant | |
|---|---|---|---|---|
| Document search | Yes — semantic | Per-chat uploads | Notebooks | Varies |
| Custom agents | Yes | GPTs | No | Varies |
| Web search | Built-in (SearxNG) | Yes | No | Varies |
| Provider choice | OpenAI/Anthropic/Gemini | OpenAI | Vendor | |
| Data ownership | Full — your infra | Vendor | Vendor | |
| Self-hostable | Yes | No | No | No |
ChatGPT and NotebookLM are capable but keep your documents on a vendor's servers and lock you to one provider. Cloud AI assistants remove setup but bill monthly and hold your data. Khoj's edge is a private, self-hosted second brain — semantic search over your own documents, custom agents, and web research — with your choice of provider and your knowledge on infrastructure you own.
Deploy in Under 5 Minutes
- Click Deploy on Railway — the four services build and wire together (~5 minutes)
- Confirm
GUNICORN_WORKERSis set low (2–3) and pgvector is connected - Set
KHOJ_ADMIN_EMAIL,KHOJ_ADMIN_PASSWORD, andKHOJ_DJANGO_SECRET_KEY - Open
/server/admin, sign in, and add your OpenAI, Anthropic, or Gemini API key - Open the Khoj UI, upload documents, and start chatting with your knowledge base
Khoj indexes your files into pgvector so you can search and chat over them by meaning.
Common Use Cases
- Chat with your notes — semantic search and conversation over your own documents and Markdown
- Personal second brain — a private, searchable knowledge base you talk to, on your infrastructure
- Research assistant — combine your documents with live web search for grounded answers
- Private document Q&A — ask questions across PDFs, Word docs, and notes without a cloud service
Configuration
| Variable | Required | Description |
|---|---|---|
KHOJ_ADMIN_EMAIL | Required | Admin account email for the Khoj panel |
KHOJ_ADMIN_PASSWORD | Required | Admin password — set a strong value |
KHOJ_DJANGO_SECRET_KEY | Required | 50+ char random string securing sessions — keep stable |
GUNICORN_WORKERS | Pre-set | 2–3 to stay within Railway's RAM limit (default 6 OOMs) |
OPENAI_API_KEY / ANTHROPIC_API_KEY / GEMINI_API_KEY | Recommended | Your LLM provider key(s) |
| Postgres connection | Auto-injected | pgvector database credentials via Railway references |
Lower
GUNICORN_WORKERS, and pgvector is required. The default 6 workers exceed Railway's 8 GB plan and cause OOM crashes — set 2–3. Khoj needs the pgvector image (not stock Postgres) for semantic search; this template uses it.
Bring cloud API keys, not local models. Local inference needs a GPU Railway lacks — use OpenAI, Anthropic, or Gemini keys. Set a stable
KHOJ_DJANGO_SECRET_KEYand strong admin credentials.
Dependencies for Khoj Hosting
- Railway account — ~$10–20/month for the four services, plus LLM provider usage
- PostgreSQL with pgvector (included and wired for embeddings)
- An LLM provider key (OpenAI, Anthropic, or Gemini) for cloud inference
- Optional: search API keys (Serper, Firecrawl, Jina) for enhanced web research
Deployment Dependencies
Implementation Details
The template deploys Khoj's full four-service stack over Railway's private network: the Khoj server (Django backend with a Next.js frontend baked in), a pgvector/pgvector:pg16 PostgreSQL database for document embeddings, the Terrarium sandbox (ghcr.io/khoj-ai/terrarium) for isolated Python execution by agents, and SearxNG for privacy-focused web search. Standard Postgres lacks the pgvector extension Khoj needs for semantic retrieval, which is why the pgvector image is used.
A Railway-specific requirement is reducing GUNICORN_WORKERS from the default 6 to 2–3, since the default exceeds the 8 GB plan limit and causes out-of-memory crashes; with cloud LLM APIs, Khoj then runs comfortably in around 4 GB. Local model inference via Ollama is impractical on Railway due to the RAM and GPU constraints, so cloud provider keys (OPENAI_API_KEY, ANTHROPIC_API_KEY, or GEMINI_API_KEY) are the practical path, added as environment variables or through the admin panel at /server/admin.
KHOJ_ADMIN_EMAIL and KHOJ_ADMIN_PASSWORD create the admin account, and KHOJ_DJANGO_SECRET_KEY (a long random string) must remain stable to keep sessions valid. Documents, embeddings, chat history, and agent configurations persist in the pgvector database, which is the component to back up.
Frequently Asked Questions
Why does Khoj need pgvector? It stores document embeddings and runs semantic search through the pgvector extension, which Railway's managed Postgres doesn't include. This template uses the pgvector/pgvector:pg16 image so retrieval by meaning works.
Why is Khoj running out of memory? The default 6 Gunicorn workers exceed Railway's 8 GB plan. Set GUNICORN_WORKERS to 2–3, and with cloud LLM APIs Khoj runs comfortably in about 4 GB. This template sets a sensible worker count.
Can I use local models? Not practically on Railway — local inference via Ollama needs a GPU and more RAM than the plan allows. Use cloud API keys (OpenAI, Anthropic, or Gemini) instead.
How do I add my API key? Sign in at /server/admin and add your OpenAI, Anthropic, or Gemini key under Server Chat Settings, or set it as an environment variable on the Khoj service.
Does my data persist? Yes — documents, embeddings, chat history, and agents live in the pgvector database and survive redeploys. Back it up to preserve your knowledge base.
Why Deploy Khoj 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 Khoj on Railway you get a private AI second brain with the hard parts solved — four services pre-wired, pgvector for semantic search, worker count tuned for Railway's RAM, and BYOK cloud providers. Chat with your documents and build custom agents, self-hosted on infrastructure you own.
Template Content
RESEND_EMAIL
Sender email for magic link delivery
RESEND_API_KEY
Resend API key for magic link emails
KHOJ_ADMIN_EMAIL
Admin account email
KHOJ_ADMIN_PASSWORD
Admin password (bootstrap-only)
Terrarium
ghcr.io/khoj-ai/terrariumSearxNG
searxng/searxng:latest