DDEV: How can I add the pgvector extension to the postgres container?

I wanted a way to add the ankane/pgvector PostgreSQL extension to the DDEV postgres service. I created a .ddev/db-build/Dockerfile file and added the code from https://github.com/pgvector/pgvector/blob/v0.5.1/Dockerfile (except the FROM), but had errors during building the container (make does not work). Maybe someone has an example or a hint?

The main problem was, that I had to install the build-essential package. Added dbimage_extra_packages: [build-essential] in the config.yaml.

And on the image you can see my Docker configuration to have pgvector in place:
enter image description here

Content of the Dockerfile.vector:

ARG PG_MAJOR=15

COPY ./pgvector-0.5.1 /tmp/pgvector

RUN apt-get update && \
        apt-mark hold locales && \
        apt-get install -y --no-install-recommends build-essential postgresql-server-dev-$PG_MAJOR && \
        cd /tmp/pgvector && \
        make clean && \
        make OPTFLAGS="" && \
        make install && \
        mkdir /usr/share/doc/pgvector && \
        cp LICENSE README.md /usr/share/doc/pgvector && \
        rm -r /tmp/pgvector && \
        apt-get remove -y build-essential postgresql-server-dev-$PG_MAJOR && \
        apt-get autoremove -y && \
        apt-mark unhold locales && \
        rm -rf /var/lib/apt/lists/*

For more information about the customization of Docker images look here: https://ddev.readthedocs.io/en/latest/users/extend/customizing-images/#adding-extra-dockerfiles-for-webimage-and-dbimage

An other way is to create an additional docker compose yaml in the .ddev directory. This way we can use the ankane/pgvector image:

enter image description here

Example of the docker-compose.pgvector.yaml file:

services:
  pgsql:
    image: 'ankane/pgvector:latest'
    ports:
      - '${FORWARD_DB_PORT:-5432}:5432'
    environment:
      PGPASSWORD: 'db'
      POSTGRES_DB: 'db'
      POSTGRES_USER: 'db'
      POSTGRES_PASSWORD: 'db'
    labels:
      com.ddev.site-name: ${DDEV_SITENAME}
      com.ddev.approot: $DDEV_APPROOT
    volumes:
      - ".:/mnt/ddev_config"
      - 'pgsql:/var/lib/postgresql/data'
    healthcheck:
      test:
        - CMD
        - pg_isready
        - '-q'
        - '-d'
        - 'db'
        - '-U'
        - 'db'
      retries: 3
      timeout: 5s

volumes:
  pgsql:

Leave a Comment