AI Workflows

PGVector Tutorial: Using Postgres as a Vector Database

Learn how to use PGVector to transform Postgres into a vector database for embedding and similarity search applications.

4 min read

Learn how to transform PostgreSQL into a vector database using PGVector! This tutorial will cover the essentials: setting up PGVector, creating a vector-enabled database, storing embeddings, and performing similarity searches. By the end, you’ll be able to leverage Postgres for embeddings and semantic search tasks.

Setting Up Postgres and PGVector

To use PGVector, you need to prepare your PostgreSQL environment and install the necessary extension.

prerequisites

  • PostgreSQL installed and running. Recommended version: 11 or later.
  • Administrative access to your Postgres environment (e.g., psql or a GUI client like pgAdmin).
  • Ability to download and install extensions from open-source repositories (e.g., GitHub).

Installing PGVector

Start by installing the PGVector extension:

  1. Clone the repository or download pre-built binaries:

    bash
    git clone https://github.com/pgvector/pgvector.git
    cd pgvector
  2. Compile and install PGVector:

    • On Linux/Mac:
      bash
      make
      sudo make install
    • On Windows: Follow instructions in the repository that match your platform.
  3. Verify installation in Postgres: Launch psql and run:

    sql
    SELECT * FROM pg_available_extensions WHERE name = 'vector';

    This will confirm if PGVector is available.

Creating a Vector-Enabled Database

With PGVector installed, let's create a database that supports vector operations.

steps

  1. Connect to Postgres:

    bash
    psql -h localhost -U postgres

    Replace localhost with the appropriate host, and postgres with your username.

  2. Create a new database:

    sql
    CREATE DATABASE vector_tutorial_db;
  3. Enable the PGVector extension:

    sql
    \c vector_tutorial_db
    CREATE EXTENSION IF NOT EXISTS vector;
  4. Define a vector-supported table: For example, create a table for items with an embedding column:

    sql
    CREATE TABLE items (
        id SERIAL PRIMARY KEY,
        content TEXT,
        embedding VECTOR(1536)
    );

    Adjust the dimensionality (1536) to match your embedding model's output.

Generating and Storing Embeddings

Next, we'll generate embeddings for text data and insert them into the Postgres database.

Install Required Python Packages

pip install psycopg2-binary numpy

The Python script below illustrates how to connect to Postgres, generate embeddings (using a model of choice), and insert them into the database.

python
import os
import psycopg2
import numpy as np

# Configure PostgreSQL connection
conn = psycopg2.connect(
    dbname="vector_tutorial_db",
    user="postgres",
    password="postgres",  # Replace with your actual password
    host="localhost"
)

texts = ["Sample text 1", "Another piece of information", "Data for similarity search"]

# Simulated embedding generation
embeddings = [np.random.rand(1536) for _ in texts]

# Insert embeddings into the database
with conn.cursor() as cur:
    for text, embedding in zip(texts, embeddings):
        cur.execute(
            "INSERT INTO items (content, embedding) VALUES (%s, %s)",
            (text, list(embedding))
        )
    conn.commit()

conn.close()

Replace the embedding generation logic with a call to your embedding model (e.g., OpenAI, Hugging Face).

PGVector allows for efficient similarity searches using operators specifically designed for vectors.

steps

  1. Connect to the vector-enabled database:

    bash
    psql -h localhost -U postgres -d vector_tutorial_db
  2. Run similarity queries: Use PGVector’s <-> operator for cosine similarity. For example:

    sql
    SELECT id, content
    FROM items
    ORDER BY embedding <-> ARRAY[0.1, 0.2, ..., 0.8]::VECTOR
    LIMIT 5;

    Replace ARRAY[...] with the embedding vector you are comparing against.

  3. Fetch and use results: Integrate into your application logic by fetching the results and displaying the most relevant matches.

Common Issues with Vector Indexing

PGVector works out-of-the-box for small datasets, but performance can degrade significantly as the number of entries grows.

Takeaways

PGVector extends PostgreSQL’s capabilities, enabling you to integrate embeddings and semantic search directly into your database. It avoids the need for dedicated vector services while leveraging Postgres for efficient storage and retrieval. By handling vector indexing appropriately, you can scale similarity searches for AI-driven applications with ease.

FAQ

How do I update the dimensionality of vector columns in PGVector?

The dimensionality of a vector column is fixed at creation. To change it, you'll need to recreate the table with the desired dimensionality, then migrate existing data.


Can PGVector handle non-numeric embeddings?

No. PGVector requires embeddings to be numeric arrays (floating-point) for distance computations.


What are the pros and cons of using PGVector over dedicated vector databases?

Pros: Simplifies development by using PostgreSQL as a single data store.
Cons: May not match the performance of purpose-built vector databases like FAISS at larger scales.


Official reference: pgvector.