16. AI and Databases#
Databases hold the data AI systems need most, and AI is changing how people query them. This chapter covers generating SQL from natural language, storing and searching vectors inside PostgreSQL, and keeping database access controlled when AI is the one asking.
Text-to-SQL: Models can turn questions into SQL when given schema context, but every query needs validation.
Vectors in PostgreSQL: The pgvector extension stores embeddings next to relational data for hybrid queries.
Data Access Control: Permissions, row-level security, and auditing limit what AI-generated queries can reach.
Chapter Video
This explainer shows how a model uses schema context to turn a business question into SQL, and why the result still needs checking.
AI & Text to SQL: How LLMs & Schema Power Data Analytics (9:00) by IBM Technology
Hosted on YouTube; property of its original creator, embedded here for educational reference only.
Learning Objectives
By the end of this chapter, you will be able to:
Provide schema context that helps a model generate correct SQL.
Validate AI-generated SQL before running it.
Store embeddings in PostgreSQL with pgvector and combine vector and relational filters.
Apply read-only roles, row-level security, and auditing to AI database access.
Build a text-to-SQL tool with query validation in Python.
Chapter Flow
Glossary
# |
Term |
Explanation |
|---|---|---|
1 |
Structured Query Language (SQL) |
The standard language for querying and changing relational databases. |
2 |
Text-to-SQL |
Generating SQL queries from natural-language questions. |
3 |
Schema context |
Table and column descriptions given to a model so it can write correct queries. |
4 |
Query validation |
Checking a generated query’s syntax, tables, and operations before running it. |
5 |
Read-only role |
A database account that can query data but cannot change it. |
6 |
pgvector |
A PostgreSQL extension that adds a vector type and similarity search. |
7 |
Vector column |
A table column that stores embeddings. |
8 |
Hybrid query |
A query that combines vector similarity with relational conditions. |
9 |
Row-level security |
Database rules that limit which rows each user can see. |
10 |
Audit log |
A record of who ran which queries and when. |
11 |
SQL injection |
An attack that inserts malicious SQL through user input. |
12 |
Parameterized query |
A query that passes values separately from SQL text to prevent injection. |
13 |
Database authorizer |
A database callback checking permitted operations during preparation. |
14 |
Execution accuracy |
Agreement between query results and reviewed expected results. |
15 |
Query budget |
A limit on work, time, or returned data. |
16 |
Cosine distance |
A dissimilarity ordering derived from vector direction. |
17 |
Embedding version |
The identity of a representation configuration. |
18 |
Trusted identity context |
Caller information established outside model output. |
19 |
Query audit |
Recorded policy, operation, outcome, and access evidence. |
20 |
Read-only access |
Permission to read allowed data without changing it. |
21 |
Result equivalence |
Two queries returning the same required result under a stated comparison. |