SQL and Postgres Basics
Enough SQL to store conversations, metadata and embeddings in Postgres, which is where many production RAG systems keep their data.
Beginner · about 10 hours · difficulty 2/5
AI applications still need ordinary data: users, conversations, documents, feedback, costs. Postgres handles all of that, and with the pgvector extension it can also store embeddings, which is why it is our recommended starting point for vector search later in this track.
What to learn
- Querying:
SELECT,WHERE,ORDER BY,LIMIT,JOIN,GROUP BY. - Schema design: tables for documents, chunks and conversations; primary and foreign keys.
- Indexes: why a query is slow and how an index fixes it. Vector indexes follow the same idea.
- JSONB columns: handy for storing flexible metadata or raw model responses.
- Parameterized queries: never build SQL by concatenating strings, especially with text that came from a user or a model.
- An ORM or query builder: SQLAlchemy, Prisma or Drizzle are common, but know the SQL they generate.
Pitfalls
Letting an LLM write and execute SQL against production without guardrails is dangerous. If you build text-to-SQL features later, use a read-only role and limits.
Good enough to move on
You can design a small schema for documents and chat messages, insert and query it from code with parameterized queries, and add an index to speed up a filter.
Key concepts
- SELECT, JOIN, GROUP BY
- Schema design and keys
- Indexes
- JSONB columns
- Parameterized queries: Prevents SQL injection
- ORMs (SQLAlchemy, Prisma, Drizzle)
Mini project
Create a Postgres schema for conversations and messages, insert sample chats from code, and write a query that returns the 10 most active users with their message counts.
Check yourself
Why is concatenating model-generated text into a SQL string dangerous, and what is the fix?
Free resources
- PostgreSQL tutorial (postgresql.org)
- SQLBolt interactive lessons (SQLBolt)