Adding pgvector to Fly Postgres

Working with “vector” databases is extremely popular right now due to various AI and Machine Learning tasks they can help improve. Luckily the Supabase team open sourced their pgvector library to make storing and working with them built right into Postgres!

Unfortunately the default Fly Postgres Image does not include it by default. So I set out to do this:

Create a Dockerfile

FROM flyio/postgres-flex:15.2

# Install build dependencies
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        build-essential \
        curl \

# Set the pgvector version

# Download and extract the pgvector release, build the extension, and install it
RUN curl -L -o pgvector.tar.gz "${PGVECTOR_VERSION}.tar.gz" && \
    tar -xzf pgvector.tar.gz && \
    cd "pgvector-${PGVECTOR_VERSION}" && \
    make && \
    make install

# Clean up build dependencies and temporary files
RUN apt-get remove -y build-essential curl postgresql-server-dev-all && \
    apt-get autoremove -y && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/* && \
    rm -rf /pgvector.tar.gz /pgvector-${PGVECTOR_VERSION}

Chat GPT helped me write this, crazy right?

Build and Publish that file, replacing my user/image name flyjason/fly-pg-pgvector with your own

$ docker build . -t flyjason/fly-pg-pgvector --platform "linux/amd64"
$ docker push flyjason/fly-pg-pgvector

Then lets create a Fly Postgres Instance!

$ flyctl postgres create --image-ref flyjason/fly-pg-pgvector

And thats it! You can use this same method to add any plugin you might be missing from the default Postgres setup, without missing all the nice tooling Fly provides!