POSTGRESQL
Vectors

A Beginner's Guide to Vector Search Using pgvector

intro

In this blog, we’ll explore the PostgreSQL vector search capability offered by pgvector. Join the journey!

Tools used in the tutorial
Tool Description Link

These days, vectors are everywhere, and we’re not only talking about AI-enhanced vector capabilities but about vectors in general. Vector databases are a thing too — but what to do when you need to search through vector-based data without using them?

MariaDB has MariaDB Vector — PostgreSQL has pgvector!

What is pgvector?

pgvector is the PostgreSQL answer to vector search: in essence, it is a Postgres extension allowing us to query, store, and index vector data. Since PostgreSQL doesn’t come with native vector capabilities, the team behind the database has decided to introduce vector similarity search as an extension and introduced it starting from PostgreSQL 11.

How and When to Use pgvector?

These are the prerequisites to use pgvector in PostgreSQL:

Once pgvector is installed enable it by creating the extension like so:

Copy
        
1 CREATE EXTENSION pgvector;

If the query is successful (it acts on the pgvector you’ve installed previously), you should be good to go.

To use pgvector, you now need:

  1. A table bearing the vector data type.
  2. Text to search through.
  3. Embeddings (vector similarity searches for vector embeddings)

An image by OpenAI nicely answers many questions related to the points above:

Using the Embedding Model to Turn Text Into a Vector Embedding. Image by OpenAI
Using the Embedding Model to Turn Text Into a Vector Embedding. Image by OpenAI

We need text to act on (text is stored as part of the values in the database), and since we need a vector embedding, to obtain it we need to run the text through an embedding model. The result is as clear as water — we obtain a vector embedding that then gets stored in our database.

We obtain a vector embedding because an embedding is a necessity to make vector similarity search tick — pgvector enables us to query vector embeddings, but before we query them, we must store them inside our database: text embeddings can be generated with tools like Word2Vec or even OpenAI, as can other types of embeddings (we can have document, graph, or image embeddings too.)

pgvector has a couple of prerequisites:

  1. A WITH query that fetches an embeddings vector. The query should select the necessary data from the tables in question.
  2. A SELECT query that makes use of the WITH query definition.

An example is given below — first, we fetch a vector by defining the pgvec_example function and then, we select data from the vector using the function we’ve defined using a similarity search:

Fetching and making use of an embeddings vector in PostgreSQL
Fetching and making use of an embeddings vector in PostgreSQL

Keep in mind that similarity is defined by using the <-> string operator.

Such queries wouldn’t be ran standalone — they would usually be implemented into Python (or other kind of) scripts so that they could be invoked like so:

Copy
        
1 python3 script.py

Such a functionality allows developers to combine the power of pgvector with the capabilities of their programming language of choice. Vector similarity search will find documents that are similar to your search query as well as exact matches, hence the similarity search option, or <->.

Summary

pgvector is PostgreSQL’s answer to vector similarity search. In detail, that is a PostgreSQL extension allowing us to quickly and easily implement vector-like search inside PostgreSQL since vectors aren’t available by default.

FAQ

What is pgvector?

pgvector is an extension by Postgres. It allows us to implement vector similarity searches in our database.

How to use pgvector?

To use pgvector, enable the extension, generate vector embeddings using OpenAI or other tools, store them in a database, and act on them using SQL queries that can also be part of Python or other kinds of scripts.

Where can I learn more about database extensions?

To learn more about database extensions, follow our blog, as well as keep yourself up to date by reading a book or two Hacking MySQL is a good place to start for those looking into a relational database.

Dbvis download link img

About the author
LukasVileikisPhoto
Lukas Vileikis
Lukas Vileikis is an ethical hacker and a frequent conference speaker. He runs one of the biggest & fastest data breach search engines in the world - BreachDirectory.com, frequently speaks at conferences and blogs in multiple places including his blog over at lukasvileikis.com.
The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

SQL Boolean Type: How to Use It in All Major Relational Databases

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2025-09-23
title

Postgres List Schemas: 3 Different Approaches

author Antonello Zanini tags POSTGRESQL 5 min 2025-09-15
title

pgvectorscale: An Extension for Improved Vector Search in Postgres

author Antonello Zanini tags AI POSTGRESQL Vectors 9 min 2025-09-03
title

PostgreSQL Index Mechanism: A Guide to Database Performance Optimization

author Leslie S. Gyamfi tags POSTGRESQL 8 min 2025-09-02
title

UUIDv7 in PostgreSQL 18: What You Need to Know

author Antonello Zanini tags POSTGRESQL 8 min 2025-09-01
title

SQL Server Vector Data Type, Search, and Indexing

author Antonello Zanini tags AI SQL SERVER Vectors 8 min 2025-08-25
title

PostgreSQL ISNULL Equivalent: COALESCE, CASE, and NULLIF

author TheTable tags POSTGRESQL 5 min 2025-08-19
title

SQL IS NOT NULL Condition: Definitive Guide

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-08-13
title

Can a Primary Key Be NULL in an SQL Table?

author TheTable tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 5 min 2025-08-12
title

ISNULL vs COALESCE: Comparing NULL Handling Functions

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-08-06

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.