1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

docker上のPostgreSQLでベクトルデータを利用する環境を構築する

Posted at

はじめに

生成AIの応答精度を高めるRAGを利用するためにベクトルデータベースを利用する必要がある。
ベクトルデータベースとしては、Qdrantのように特化して設計されたものもあるが、今回はタイトル通りDocker上のPostgreSQLを使って環境構築を行う。
過去に書いているpg_ivmの記事と考え方は同じ。

確認環境

  • WSL2(Ubuntu22.04.3 LTS)
  • PostgreSQL 17.4-1
  • Docker (27.5.1)
  • Docker Compose(v2.32.4)
  • vector (0.8.0)

フォルダ構成

[projectfolder]
 ├─ compose.yml
 ├─ Dockerfile
 └─ docker-entrypoint-initdb.d
    └─ 90_vector.sql (extentionを登録するSQL)

ソース

compose.yml
services:
  db:
    image: postgres-vector-plugin:1.0
    build:
      context: .
    restart: always
    volumes:
      - postgresql:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: postgres
    tty: true

volumes:
  postgresql:
Dockerfile
# postgresのバージョン17最新
from postgres:17.4-bookworm

# パッケージ更新&パッケージのビルドで必要なもの
RUN apt-get update && apt-get install -y \
    git \ 
    make \ 
    gcc \
    postgresql-server-dev-17

# ------ pgvector導入
RUN git clone https://github.com/pgvector/pgvector.git /usr/src/pgvector \
    && cd /usr/src/pgvector \
    && make \
    && make install \
    && cd / 

# EXTENTION登録SQLをコピー
COPY ./docker-entrypoint-initdb.d/90_vector.sql /docker-entrypoint-initdb.d

# 使わないので削除
RUN apt-get purge -y \
    git \ 
    make \ 
    gcc \
    postgresql-server-dev-17
# ファイルも使わないので一応削除
RUN rm -rf /usr/src/pgvector 
docker-entrypoint-initdb.d / 90_vector.sql
CREATE EXTENSION IF NOT EXISTS vector;

起動コマンド

docker compose up -d

ざっくり解説

  • composeファイルのimage名は何でもよいが、初期化するとき等に便利
  • Dockerfileでビルドして、拡張機能登録用のSQLファイルを/docker-entrypoint-initdb.d にコピーしてコンテナ作成時に実行している(バインドマウントでフォルダ共有しても良い)

Extensionが導入されているかの確認

postgres=# SELECT * FROM pg_extension;
  oid  | extname | extowner | extnamespace | extrelocatable | extversion | extconfig | extcondition
-------+---------+----------+--------------+----------------+------------+-----------+--------------
 13569 | plpgsql |       10 |           11 | f              | 1.0        |           |
 16384 | vector  |       10 |         2200 | t              | 0.8.0      |           |

おまけ

Dockerfileで、postgresのイメージを利用して、必要なパッケージを利用して…という方法で展開しているが、イメージに pgvectorを利用(from pgvector/pgvector:latest)して、構築することも可能らしい(動作は未確認)

終わりに

この環境に対して、LlamaIndexを利用したpythonコードを展開する予定です。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?