GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing computingforgeeks.com — Install pgvector on PostgreSQL 17 (Rocky / Ubuntu) [With Examples] pgvector turns PostgreSQL into a vector database. If you already run PostgreSQL for your applications, you can store and query vector embeddings without adding another service to your stack. That means semantic search, recommendation engines, and RAG pipelines all running inside the database you already manage and back up. Original content from computingforgeeks.com - post 74809This guide covers installing pgvector on PostgreSQL 17 for both Rocky Linux 10 and Ubuntu 24.04, creating vector columns, building indexes (HNSW and IVFFlat), running similarity searches, and practical demos you can adapt for your own projects. Tested March 2026 on Rocky Linux 10.1 (SELinux enforcing) and Ubuntu 24.04 LTS with PostgreSQL 17.9, pgvector 0.8.2 What You Need
pgvector itself is lightweight. It adds no background processes and uses memory only when you query or build indexes. Install pgvectorpgvector is available as a prebuilt package from the official PostgreSQL repository (PGDG) on both OS families. No compilation needed. Rocky Linux 10 / AlmaLinux 10 / RHEL 10Install pgvector from the PGDG repository:
The package name follows the pattern Ubuntu 24.04 / Debian 13On Debian-based systems, install the pgvector package:
On Debian/Ubuntu the pattern is Enable the ExtensionAfter installing the package, enable the extension inside the database where you need vectors:
Verify the extension is active and check the version:
The output confirms pgvector is loaded:
Vector Data Typespgvector 0.8.2 supports four data types for different use cases:
For most AI embedding models (OpenAI, Ollama, Cohere), use Create Tables with Vector ColumnsA vector column stores fixed-length arrays of floats. Specify the number of dimensions when creating the column. Common embedding dimensions are 384 (MiniLM, nomic-embed-text), 768 (BERT), 1536 (OpenAI text-embedding-ada-002), and 3072 (OpenAI text-embedding-3-large). Connect to PostgreSQL:
Create a table with a 384-dimension vector column (matches Ollama’s
Distance Operatorspgvector provides operators for different distance metrics. The choice depends on how your embedding model was trained:
Cosine distance (
Find items most similar to “cat” using cosine distance:
“Dog” ranks highest because its vector is closest to “cat” in direction:
All three distance operators side by side on the same data:
The results show how each metric measures distance differently. L2 uses absolute magnitude, cosine ignores magnitude and compares direction only, and inner product combines both:
Build Vector IndexesWithout an index, pgvector performs exact (brute-force) nearest neighbor search by scanning every row. This is accurate but slow on large datasets. Indexes give you approximate nearest neighbor (ANN) search that is dramatically faster at the cost of slightly less perfect recall. HNSW Index (Recommended)HNSW (Hierarchical Navigable Small World) is the preferred index type for most workloads. It builds a multi-layer graph and delivers consistently fast queries without the training step that IVFFlat requires.
Key parameters:
At query time, control the search accuracy vs speed tradeoff with
IVFFlat IndexIVFFlat divides vectors into clusters (lists) and searches only the nearest clusters at query time. It requires data in the table before building (unlike HNSW which can be built on an empty table). Use IVFFlat when memory is constrained because it uses less memory than HNSW.
The
Which Index to Choose?
Start with HNSW unless you have a specific reason to use IVFFlat. Performance Tuningpgvector benefits from the same PostgreSQL tuning you would apply for any analytics workload, plus a few vector-specific settings. Edit the PostgreSQL configuration:
On Ubuntu, the path is Key settings to adjust:
Restart PostgreSQL after changes:
On Ubuntu, the service name is Practical Demo: Semantic Search with 1,000 DocumentsThis demo creates a table with 1,000 documents, builds an HNSW index, and runs similarity queries to show real query plans and timing.
Create and populate the table with random 384-dimension vectors (in production, these come from an embedding model like Ollama’s nomic-embed-text):
Build the HNSW index:
Check the index size:
The HNSW index on 1,000 rows with 384 dimensions takes about 2 MB:
Run a similarity search with EXPLAIN ANALYZE to see the query plan:
The query completes in under 1ms on 1,000 rows:
Generate Embeddings with OllamaTo store real embeddings (not random vectors), you need an embedding model. Ollama runs embedding models locally. The Pull the embedding model (assumes Ollama is installed):
Generate an embedding via the API:
This returns
This pattern (embed with Ollama, store in pgvector) is the foundation of a self-hosted RAG pipeline. No data leaves your network, no API costs, and your existing PostgreSQL backup strategy covers the vectors too. Embedding Model Dimensions ReferenceWhen creating your vector column, the dimension must match the model you use. Here are the common embedding models and their output dimensions:
Rocky Linux 10 vs Ubuntu 24.04The pgvector extension works identically on both platforms. The differences are in package names, config paths, and service management:
Production ConsiderationsA few things to keep in mind when running pgvector in production:
Going FurtherWith pgvector installed, you have the building blocks for several AI-powered workflows:
Keep reading
Ubuntu
Upgrade Ubuntu 24.04 to Ubuntu 26.04 LTS (Step by Step)
Security
UFW Firewall Commands with Examples on Ubuntu 26.04 / 24.04
AI
Ollama Models Cheat Sheet: Best Models by VRAM, Tested (PDF)
Databases
Chronograf Tutorial: Dashboards and Alert Rules for InfluxDB
AI
Self-Host LibreChat with Ollama, LiteLLM and SearXNG Web Search
Debian
Install sudo-rs: Memory-Safe sudo for Linux
Leave a Comment Cancel reply
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |