How to Install PostgreSQL with pgvector on Contabo VPS: Self-Hosted AI Embeddings & RAG Guide
Contabo High-Performance Cloud VPS
Massive 4 vCPU cores, 8GB RAM, and 32TB monthly traffic. Ideal for Linux developers.
Cut cloud vector database costs by 90%. Learn how to build and scale your own self-hosted vector database using PostgreSQL and the pgvector extension on a high-RAM Contabo VPS.
- Eliminates prohibitive recurring SaaS vector database bills (Pinecone, Qdrant Cloud, Weaviate Cloud)
- Unifies relational business SQL tables with high-dimensional vector search inside a single transactional engine
- Contabo Cloud VPS provides up to 32 GB RAM, ideal for keeping massive HNSW vector indexes resident in memory
The Explosion of Enterprise AI and Vector Search
The generative AI revolution has permanently transformed software architecture. Whether you are engineering Retrieval-Augmented Generation (RAG) knowledge assistants, semantic enterprise document search, customer recommendation engines, or fraud detection systems, your application must store and query high-dimensional mathematical vectors (embeddings).
Embedding models—such as OpenAI's text-embedding-3-small (1536 dimensions) or open-source HuggingFace models like bge-large-en-v1.5 (1024 dimensions)—convert unstructured text, PDF documents, and customer tickets into arrays of floating-point numbers. To find relevant context for an AI prompt, your database must calculate nearest-neighbor distances across millions of vectors in milliseconds.
Proprietary vector SaaS providers like Pinecone, Weaviate Cloud, and Qdrant Cloud charge steep recurring subscription fees that escalate aggressively as your vector counts grow. Fortunately, PostgreSQL—the world's most trusted open-source relational database—possesses the official pgvector extension. With pgvector, you can store vectors alongside your users, products, and billing records in standard SQL tables.
Because vector similarity search achieves optimal performance when index graphs are cached entirely in memory, a Contabo Cloud VPS ($8.95 - $14.95/mo for 16GB to 30GB RAM) provides the ideal high-capacity foundation for AI infrastructure.
Hardware Sizing: Why Contabo RAM Wins for Vector Indexes
Unlike standard text indexing (B-Tree), vector indexes like HNSW (Hierarchical Navigable Small World) build multi-layered graphs connecting multidimensional points. If an HNSW index spills over onto disk, vector query latency jumps from 4 milliseconds to over 250 milliseconds.
| Vector Count (1536-dim) | Raw Vector Data | HNSW Index in RAM | Recommended Server |
|---|---|---|---|
| 100,000 Vectors | ~600 MB | ~1.2 GB RAM | Contabo Cloud VPS 1 ($4.95/mo - 8 GB RAM) |
| 1,000,000 Vectors | ~6.1 GB | ~11.5 GB RAM | Contabo Cloud VPS 2 ($8.95/mo - 16 GB RAM) |
| 3,000,000 Vectors | ~18.3 GB | ~32 GB RAM | Contabo Cloud VPS 3 ($14.95/mo - 30 GB RAM) |
Step 1: Installing PostgreSQL 16 on Ubuntu 24.04
Connect to your Contabo VPS via SSH and install the official PostgreSQL APT repository:
ssh root@YOUR_CONTABO_IP
apt update && apt upgrade -y
apt install -y curl gnupg lsb-release build-essential git postgresql-common
/usr/share/postgresql-common/pgdg/apt.postgresql.org.sh -y
apt update
apt install -y postgresql-16 postgresql-server-dev-16
Step 2: Compiling pgvector from Official Source
Building pgvector from source ensures support for hardware vector instructions (AVX-512 / AVX2):
cd /tmp
git clone --branch v0.7.4 https://github.com/pgvector/pgvector.git
cd pgvector
make
make install
Step 3: Initializing Extension & AI Database
Switch to the PostgreSQL system user and launch the psql terminal:
sudo -u postgres psql
CREATE DATABASE enterprise_rag;
c enterprise_rag;
CREATE EXTENSION vector;
dx
You should see vector listed with version 0.7.4.
Step 4: Defining Vector Schemas & HNSW Indexing
Create a production table for storing document chunks, source metadata, and 1536-dimensional embeddings:
CREATE TABLE knowledge_chunks (
id BIGSERIAL PRIMARY KEY,
document_title TEXT NOT NULL,
chunk_text TEXT NOT NULL,
source_url TEXT,
embedding vector(1536),
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
To enable microsecond similarity lookups, create an HNSW index using cosine distance (vector_cosine_ops):
CREATE INDEX idx_chunks_embedding_hnsw
ON knowledge_chunks
USING hnsw (embedding vector_cosine_ops)
WITH (m = 16, ef_construction = 64);
Step 5: Querying Similar Vectors with SQL
To retrieve the top 5 most semantically relevant context chunks for a user query:
SELECT id, document_title, chunk_text,
1 - (embedding <=> '[0.0125, -0.0432, 0.0891, ...]') AS cosine_similarity
FROM knowledge_chunks
ORDER BY embedding <=> '[0.0125, -0.0432, 0.0891, ...]'
LIMIT 5;
Step 6: PostgreSQL Memory Optimization (/etc/postgresql/16/main/postgresql.conf)
To guarantee that vector calculations execute entirely in Contabo's memory:
shared_buffers = 4GB
work_mem = 64MB
maintenance_work_mem = 2GB
effective_cache_size = 12GB
random_page_cost = 1.1
effective_io_concurrency = 200
Apply changes with systemctl restart postgresql.
Connecting with Python (LangChain & LlamaIndex)
from langchain_community.vectorstores import PGVector
from langchain_openai import OpenAIEmbeddings
CONNECTION_STRING = "postgresql+psycopg2://postgres:YOUR_PASS@YOUR_CONTABO_IP:5432/enterprise_rag"
db = PGVector.from_documents(
embedding=OpenAIEmbeddings(),
documents=docs,
collection_name="knowledge_chunks",
connection_string=CONNECTION_STRING,
)
Recommended Reading & Related Guides
Frequently Asked Questions (FAQ)
What is the difference between HNSW and IVFFlat indexes?
HNSW provides higher search recall and lower query latency without requiring prior training, though it consumes more RAM. IVFFlat uses less memory but requires re-clustering as datasets grow. For 95% of RAG applications, HNSW on a high-RAM Contabo VPS is the gold standard.
How does pgvector performance compare to Pinecone?
In independent benchmarks with 1 million vectors, PostgreSQL with pgvector achieved 4ms P95 query latency—matching or beating Pinecone standard pods while costing 90% less.
Final Verdict
Self-hosting PostgreSQL with pgvector on a Contabo Cloud VPS liberates your AI development from expensive SaaS billing meters. You gain full SQL flexibility, enterprise security, and blazing vector search performance at a fraction of hosted cloud pricing.
Video Walkthrough & Official References
PostgreSQL and Redis Caching on Ubuntu Linux Tutorial • Sadman Sakib
Editorial Verdict: Pros & Cons
What We Love (Pros)
- Eliminates prohibitive recurring SaaS vector database bills (Pinecone, Qdrant Cloud, Weaviate Cloud)
- Unifies relational business SQL tables with high-dimensional vector search inside a single transactional engine
- Contabo Cloud VPS provides up to 32 GB RAM, ideal for keeping massive HNSW vector indexes resident in memory
- Full compatibility with popular AI orchestration frameworks including LangChain, LlamaIndex, and OpenAI
Drawbacks (Cons)
- Requires manual index selection (HNSW vs IVFFlat) and PostgreSQL memory tuning for optimal recall latency
Deploy Your Contabo Cloud Server
Includes 4 vCPU Cores, 8GB RAM, 50GB NVMe SSD, and 32TB Outbound Traffic.
Written by Alex Vance
Cloud Infrastructure Architect
Alex and the ServerCraft engineering lab run routine benchmarks on over 25 web hosting providers, maintaining strict objectivity and reproducibility.
Recommended Next Reads
How to Run Ollama & Open WebUI on Contabo Cloud VPS: Private Self-Hosted AI Assistant
Stop leaking proprietary code and confidential data to public AI APIs. Run local LLMs like Llama 3 and Mistral on high-RAM Contabo VPS with Open WebUI.
How to Connect Multiple VPS Servers with Private WireGuard Mesh Network
Stop exposing private databases and backend APIs to the public internet. Learn how to connect multiple VPS nodes securely over an encrypted WireGuard mesh tunnel.
How to Self-Host LiteLLM Proxy on Contabo VPS: Unified AI Gateway with Cost Tracking & Rate Limits
Stop juggling messy API keys across OpenAI, Anthropic, and Groq. Learn how to self-host LiteLLM Proxy on a Contabo Cloud VPS for centralized cost tracking, failover load balancing, and team rate-limiting.