Implementing the pgvector extension for a PostgreSQL database

Johannes Johansson
3 min readSep 15, 2023

If you’re looking to implement vectorized search using PostgreSQL and Python, you’re in luck! In this article, we’ll explore how you can set up a PostgreSQL database with the pgvector extension and utilize it to perform vectorized searches.

To get started, we’ll be using an existing demo repository called “pgvector-demo”, which showcases the implementation of vectorized search. You can find the repository on GitHub and clone it to your local machine (https://github.com/johannesocean/pgvector-demo).

Firstly, we need to create a docker-compose.yml file with the necessary services. Create a file named docker-compose.yml and add the following contents:

services:
db:
hostname: db
image: ankane/pgvector
ports:
- 5432:5432
restart: always
environment:
- POSTGRES_DB=vectordb
- POSTGRES_USER=testuser
- POSTGRES_PASSWORD=testpwd
- POSTGRES_HOST_AUTH_METHOD=trust
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql

In this docker-compose.yml file, we define a service called db which is based on the ankane/pgvector Docker image. The service exposes port 5432 to interact with the database and sets up environment variables for the database name, user, password, and authentication method. Additionally, we mount an init.sql file to the /docker-entrypoint-initdb.d directory inside the container for initialization purposes.

--

--

Johannes Johansson

Software Developer - Data Scientist - Oceanographer #followback