Automating Postgres and pgvector Setup with Docker

A PostgreSQL and pgvector Docker setup gives you an isolated, repeatable database for local development. This guide shows you how to run Docker with PostgreSQL and pgvector for efficient vector operations.

Why Use pgvector with PostgreSQL?

pgvector adds vector operations directly to PostgreSQL. It supports machine-learning workloads and other applications that compare high-dimensional vectors. As a result, your application can keep vector data and relational data in one database.

Setting Up the Environment

Docker keeps the environment contained and requires little configuration. You can reproduce it on another machine. Follow these steps.

1. Docker Compose Configuration

First, create a docker-compose.yml file. It defines the PostgreSQL service and its pgvector configuration.

version: "3.9"

services:
  pgvector-db:
    env_file:
      - ./postgres-pgvector/.env
    build:
      dockerfile: postgres.Dockerfile
    container_name: postgres-pgvector
    ports:
      - "5454:5432"
    volumes:
      - db_data:/var/lib/postgresql/data
      - ./postgres/vector_extension.sql:/docker-entrypoint-initdb.d/0-vector_extension.sql
    networks:
      - default

volumes:
  db_data:

2. Dockerfile for PostgreSQL and pgvector

Next, create postgres.Dockerfile. It starts from PostgreSQL 14.1 and installs the packages that pgvector needs. The build then clones pgvector, compiles it, and installs the extension in PostgreSQL.

# Extend the official PostgreSQL 14.1 image
FROM postgres:14.1

# Install necessary dependencies for building pgvector
RUN apt-get update && apt-get install -y \
    build-essential \
    postgresql-server-dev-14 \
    git \
    clang-11 \
    llvm-11 \
    ca-certificates \  
    && update-ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# Set clang as the default compiler
ENV CC=clang-11
ENV CXX=clang++-11

# Clone and build pgvector
WORKDIR /tmp
RUN git clone https://github.com/pgvector/pgvector.git

WORKDIR /tmp/pgvector
RUN make
RUN make install

# Enable pgvector in PostgreSQL
RUN echo "shared_preload_libraries = 'pgvector'" >> /usr/share/postgresql/postgresql.conf.sample

3. Initializing the Vector Extension

Create a SQL script to initialize pgvector. Mount it at /docker-entrypoint-initdb.d in docker-compose.yml. PostgreSQL runs it when the container starts for the first time.

-- Create the 'vector' extension within the database that is set in the docker-compose.yml
CREATE EXTENSION IF NOT EXISTS vector;

The base image creates the database and applies the connection details from docker-compose.yml. Therefore, the init script needs no extra database setup.

You can now connect with your preferred SQL client by using the details in docker-compose.yml.

Conclusion

Docker makes the PostgreSQL and pgvector environment easy to reproduce. It also keeps local dependencies isolated, so development machines stay consistent and avoid avoidable conflicts.

With pgvector, your database can store and search vectors beside application data. That foundation suits many machine-learning features and helps teams scale them with fewer moving parts.

Leave A comment

Are you human? Please solve:Captcha