pgvector in PostgreSQL: Enterprise Guide to Vector Search Architecture and Operations
Prerequisites
- Working knowledge of PostgreSQL administration
- Basic understanding of embeddings and semantic search
Steps
pgvector adds vector similarity search to PostgreSQL, enabling semantic search, retrieval-augmented generation, and recommendation workloads without introducing a separate vector database. This guide explains enterprise architecture, deployment, implementation, security hardening, and operational trade-offs for production use.
Overview
pgvector is a PostgreSQL extension that stores embeddings in native tables and supports nearest-neighbor search with distance operators such as cosine, inner product, and L2. Its core purpose is to let teams run transactional data and vector search in the same database, reducing data movement, simplifying governance, and reusing PostgreSQL tooling for backup, HA, monitoring, and access control.
Enterprises use pgvector when they want semantic search or RAG over business data already governed in PostgreSQL. Typical use cases include knowledge assistants, fraud and anomaly enrichment, document retrieval, product recommendation, and support case similarity. The main value is operational consolidation: fewer platforms, simpler compliance boundaries, and easier integration with existing SQL-based applications.
Architecture
Core components usually include an embedding pipeline, PostgreSQL with the vector extension, an application or API layer, and observability tooling.
View as text
[Users] ---- 1. query ----> [Application API] [Application API] ---- 2. embed query ----> [Embedding Service] [Embedding Service] ---- 3. vector ----> [PostgreSQL + pgvector] [Application API] ---- 4. metadata filter + ANN search ----> [PostgreSQL + pgvector] [PostgreSQL + pgvector] ---- 5. rows + scores ----> [Application API] [Application API] ---- 6. response ----> [Users] [ETL / CDC] | v [Embedding Batch Jobs] ---- 7. upsert documents + embeddings ----> [PostgreSQL + pgvector] [Monitoring] <---- 8. metrics/logs ----> [PostgreSQL + pgvector]
Deployment models
- Single cluster: app tables and vectors in one PostgreSQL cluster; best for moderate scale and strong transactional coupling.
- Dedicated vector cluster: separate PostgreSQL cluster for search-heavy workloads; reduces noisy-neighbor effects.
- Managed PostgreSQL: AWS RDS for PostgreSQL, Azure Database for PostgreSQL, or Google Cloud SQL where pgvector is supported; fastest enterprise adoption path.
Data flow
- Documents or records are ingested.
- An embedding model generates vectors.
- Metadata and embeddings are stored in PostgreSQL.
- Queries are embedded at request time.
- SQL combines ANN search with tenant, ACL, and business filters.
Implementation Guide
- Install PostgreSQL 16 and pgvector on Ubuntu.
sudo apt-get update
sudo apt-get install -y postgresql-16 postgresql-server-dev-16
git clone --branch v0.7.4 https://github.com/pgvector/pgvector.git
cd pgvector
make
sudo make install
sudo systemctl restart postgresql
- Create database objects and extension.
sudo -u postgres psql <<'SQL'
CREATE DATABASE vectordb;
\c vectordb
CREATE EXTENSION vector;
CREATE ROLE app_user LOGIN PASSWORD 'S3cure-ChangeMe';
GRANT CONNECT ON DATABASE vectordb TO app_user;
CREATE TABLE documents (
id bigserial PRIMARY KEY,
tenant_id text NOT NULL,
title text NOT NULL,
body text NOT NULL,
embedding vector(1536) NOT NULL,
created_at timestamptz DEFAULT now()
);
CREATE INDEX idx_documents_tenant ON documents(tenant_id);
CREATE INDEX idx_documents_embedding_hnsw ON documents USING hnsw (embedding vector_cosine_ops);
GRANT SELECT, INSERT, UPDATE, DELETE ON documents TO app_user;
SQL
- Tune PostgreSQL for mixed OLTP and vector workloads.
# postgresql.conf
shared_buffers: 8GB
work_mem: 64MB
maintenance_work_mem: 2GB
effective_cache_size: 24GB
wal_compression: on
max_wal_size: 8GB
shared_preload_libraries: "pg_stat_statements"
- Enable TLS and restrictive authentication.
# pg_hba.conf
hostssl vectordb app_user 10.20.0.0/16 scram-sha-256
hostnossl all all 0.0.0.0/0 reject
- Query with metadata filters and ANN search.
- Use
ORDER BY embedding <=> $1 LIMIT 10for cosine distance. - Apply tenant and authorization predicates before returning rows.
Code Examples
1. Create schema and HNSW index
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE kb_chunks (
chunk_id uuid PRIMARY KEY,
doc_id uuid NOT NULL,
tenant_id text NOT NULL,
content text NOT NULL,
embedding vector(1536) NOT NULL
);
CREATE INDEX kb_chunks_embedding_hnsw ON kb_chunks USING hnsw (embedding vector_cosine_ops);
2. Kubernetes secret and config
apiVersion: v1
kind: Secret
metadata:
name: pgvector-db
namespace: ai-search
type: Opaque
stringData:
DATABASE_URL: "postgresql://app_user:REDACTED@postgres-rw.ai-search.svc.cluster.local:5432/vectordb?sslmode=require"
---
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
namespace: ai-search
data:
PGSSLMODE: "require"
VECTOR_DIM: "1536"
3. Python similarity search
import psycopg
query_embedding = [0.0123] * 1536
sql = """
SELECT id, title, 1 - (embedding <=> %s::vector) AS score
FROM documents
WHERE tenant_id = %s
ORDER BY embedding <=> %s::vector
LIMIT 5
"""
with psycopg.connect("postgresql://app_user:REDACTED@db.example.com:5432/vectordb?sslmode=require") as conn:
with conn.cursor() as cur:
cur.execute(sql, (query_embedding, "tenant-a", query_embedding))
for row in cur.fetchall():
print(row)
Security Hardening
- Encrypt in transit: require TLS with
hostsslandsslmode=requireorverify-full. - Encrypt at rest: use cloud volume encryption or LUKS; protect backups with KMS-managed keys.
- Access control: separate roles for ingestion, query, and admin; prefer SCRAM over MD5.
- Row-level security: enforce tenant isolation with PostgreSQL RLS policies.
- Secrets management: store DB credentials in Vault, AWS Secrets Manager, or Kubernetes Secrets with envelope encryption.
- Auditability: enable
log_connections,log_disconnections, andpgauditwhere required.
Comparison
| Feature | pgvector | Pinecone | Weaviate |
|---|---|---|---|
| Pricing | Low incremental cost if PostgreSQL already exists | Consumption-based managed service | Open source plus managed offerings |
| Deployment | Self-managed or managed PostgreSQL | SaaS managed | Self-managed, Kubernetes, or managed cloud |
| Scalability | Strong for moderate to large workloads; depends on PostgreSQL tuning and sharding strategy | High managed horizontal scale | Good horizontal scale with native vector focus |
| Security | Inherits PostgreSQL RBAC, TLS, RLS, audit ecosystem | Managed controls, API-key based access | TLS, auth integrations, multi-node controls |
Troubleshooting
- Extension missing.
ERROR: could not open extension control file "/usr/share/postgresql/16/extension/vector.control": No such file or directory
STATEMENT: CREATE EXTENSION vector;
Fix: install pgvector on the database host and restart PostgreSQL; verify pg_config matches the running version.
- Dimension mismatch.
ERROR: expected 1536 dimensions, not 768
CONTEXT: unnamed portal parameter $1 = '[0.11,0.22,...]'
Fix: ensure the embedding model used at query time matches the table definition; do not mix model families in one column.
- Slow query due to sequential scan.
LOG: duration: 8421.337 ms statement: SELECT id FROM documents ORDER BY embedding <=> $1 LIMIT 10;
Fix: create an HNSW or IVFFlat index, run ANALYZE, and confirm the operator class matches the distance metric.
Best Practices
Do
- Keep vectors and business metadata together when governance and joins matter.
- Use HNSW for low-latency search and benchmark recall versus latency.
- Partition by tenant or time for large datasets.
- Add metadata filters such as
tenant_id, classification, and retention state.
Don't
- Do not store embeddings from different dimensions in the same column.
- Do not expose unrestricted SQL access from application tiers.
- Do not skip recall testing; ANN speed without relevance validation causes poor outcomes.
- Do not treat pgvector as a full replacement for distributed search engines at extreme scale; validate limits with production-like load tests.
Have a project in mind?
Get an instant AI price estimate for it, or talk directly to our team.
One email a month on what we learn building with AI