A clean learning/demo repository showing how to use PostgreSQL + pgvector for vector similarity search.
Structured like a real project and easy to extend into RAG or ML pipelines.
This repository focuses on clarity, correctness, and real-world structure, rather than minimal examples.
⭐ If this repository helps you, please consider starring it and crediting the source.
- pgvector extension setup
- RAG-friendly schema (documents + chunks + embeddings)
- Python ingestion + search scripts
- Vector columns using
VECTOR(n) - Similarity search using native PostgreSQL operators
- SQL-first querying with optional Python helpers
- A project layout that mirrors production systems
- Demonstrate how pgvector enables vector similarity search inside PostgreSQL
- Show a RAG-friendly schema (documents → chunks → embeddings)
- Keep everything simple, readable, and extensible
- Serve as a foundation for:
- semantic search
- retrieval-augmented generation (RAG)
- ML / NLP pipelines
- in-database vector workloads
Not necessarily.
This repository is designed to be useful even without a local PostgreSQL installation:
- SQL scripts can be reviewed directly
- Python scripts show ingestion and search logic
- Schema and queries illustrate real pgvector usage
- The structure itself demonstrates best practices
To run the demo end-to-end, you will need PostgreSQL with the pgvector extension installed.
Only required if you want to execute the code locally.
- PostgreSQL ≥ 15
- Recommended on macOS: Postgres.app
psqlavailable in your terminaluvinstalled (Python environment & dependency manager)
.
├── sql/
│ ├── 001_create_extension.sql
│ ├── 002_create_tables.sql
│ ├── 003_sample_data.sql
│ └── 050_queries.sql
│
├── scripts/
│ ├── 00_check_connection.py
│ ├── 10_init_db.py
│ ├── 20_ingest_demo.py
│ └── 30_search_demo.py
│
├── pyproject.toml
├── uv.lock
└── README.md
uv venv
uv pip install psycopg2-binary python-dotenv
uv lockuv run python scripts/10_init_db.pyThis will:
- create the database (if needed)
- enable the vector extension
- create tables for documents, chunks, and embeddings
psql -d vectordb -f sql/050_queries.sqlThis demonstrates:
- vector distance operators
- ordering by similarity
- basic semantic search patterns
uv run python scripts/20_ingest_demo.pyThis simulates:
- chunk creation
- embedding insertion
- storage in PostgreSQL
uv run python scripts/30_search_demo.pyThis shows:
- parameterized similarity queries
- Python → SQL integration
- application-style retrieval
Run the Full Flow (Sanity Test)
- Make sure Postgres.app is running, then:
uv run python scripts/00_check_connection.py
uv run python scripts/10_init_db.py
psql -d vectordb -f sql/050_queries.sql
uv run python scripts/20_ingest_demo.py
uv run python scripts/30_search_demo.pyNotes
- Embedding dimension is set to VECTOR(3) for a minimal demo
- For real-world usage:
- use 384 / 768 / 1536 dimensions
- add ANN indexes (HNSW or IVFFlat)
- PostgreSQL + pgvector is often sufficient for small to medium-scale RAG systems
Architecture Notes
- PostgreSQL acts as both the relational store and vector store
- pgvector enables similarity search without external databases
- This pattern works well when:
- data already lives in Postgres
- operational simplicity matters
- latency requirements are moderate
Possible Extensions
- Add ANN indexes for large-scale datasets
- Plug in real embeddings (OpenAI, SentenceTransformers, etc.)
- Add a RAG layer (LLM + retriever)
- Wrap queries behind a FastAPI service
- Add Docker Compose for infra parity
This repository follows lightweight quality checks aligned with real-world applied projects:
- Linting:
pylint(configured viapyproject.toml) - CI: GitHub Actions
- Environment management:
uvwith lockfile-based reproducibility
Run linting locally:
uv sync --extra dev
uv run pylint main.py scriptsIf you use this repository — including its ideas, code, structure, or pipeline — in research, blogs, demos, internal tools, or production systems, please credit:
Sangam Kumar Singh
GitHub: https://github.com/SangiSI
Repository: https://github.com/SangiSI/pgvector-semantic-search-demo
This project is released under the MIT License, which requires attribution.
