Technical · Database

Using PGVector with a multitenant Ruby on Rails App.

January 7, 2024

Illustration of apartment buildings around a large puzzle piece labeled PGG
pgvector on a multitenat environment

This is a short one. When using the pgvector extension to enable vector searches on Ruby On Rail Applications, if the application is also multi-tenant (using Apartment or other similar gem) the normal path to add the extension to the database will be very problematic, and migrations on the tenants will fail.

Typically, adding the pgvector extension to a standard Ruby on Rails database is straightforward. But, the challenge arises in a multitenant setup where each tenant operates in a separate schema. Here, the usual methods of adding extensions can lead to failed migrations and other complications.

In those cases, the solution is running the migration with the following command

execute 'CREATE EXTENSION IF NOT EXISTS vector WITH SCHEMA pg_catalog;'

This line tells PostgreSQL to create the pgvector extension within the pg_catalog schema, which is shared across all tenants.

After that, Apartment migrations will be able to create tables or add vector columns.

using the gem neighbor will facilitate everything else.

Have you tried integrating pgvector in a multitenant environment? Share your experiences or any questions in the comments below. Your insights could be immensely helpful to the Rails community!

Originally published on Medium in 23blocks.

Related reading

More in the Digest — a weekly roundup of AI and agents news — or start with AI Strategy.