Docker Compose for Postgres with PGVector

These are the steps I took to set up Docker containers so that I can experiment with Postgres and pgvector

Create a folder called postgres-docker.

In the folder create a docker-compose.yml file and put this in it:

version: "3.8"
services:
  postgres:
    # image: postgres:13 # The ankane image (below) includes pgvector
    image: ankane/pgvector
    container_name: postgres1
    restart: always
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: sample_db
    volumes:
      - postgres-data:/var/lib/postgresql/data
      - ./dataset:/docker-entrypoint-initdb.d
       
  pgadmin:
    image: dpage/pgadmin4
    container_name: pgadmin1
    restart: always
    ports:
      - "8888:80"
    environment:
      PGADMIN_DEFAULT_EMAIL: niallkader@gmail.com
      PGADMIN_DEFAULT_PASSWORD: postgres
    volumes:
      - pgadmin-data:/var/lib/pgadmin
    depends_on:
      - postgres

volumes:
  postgres-data:
  pgadmin-data:

Create a folder named dataset inside the postgres-docker folder. In the dataset folder, create a file named init.sql and put this in it:

CREATE EXTENSION IF NOT EXISTS vector;

CREATE TABLE documents (
    id SERIAL PRIMARY KEY,
    name TEXT,
    content TEXT,
    embedding vector,
    date timestamptz DEFAULT now()
);

TODO: vectorize the content into an embedding

To start everything:

docker compose up -d

Then go to localhost:8888 and enter the pgadmin email (you@gmail.com) and password (postgres).

  1. Click on Add New Server
  2. Enter a name of Docker (this can be anything)
  3. Click on the Connection tab
    1. Enter postgres for the Host name/address (that's the host name of the container - the name of the service in the docker-compose file)
    2. Enter 5432 for the port (That's the host port that maps to the container port)
    3. Enter postgres for the username (that's the POSTGRES_USER var)
    4. Enter postgres for the password (that's the POSTGRES_PASSWORD var)

To blast everything and recreate from scratch (source):

docker compose down -v && docker compose rm -f -v && docker compose build --no-cache && docker compose up -d