LoginSignup
4
1

無料のベクトルデータベースとして Fly Postgres を使う【pgvector】

Last updated at Posted at 2023-07-09

はじめに

fly.io では PostgreSQL を使うことができ, 無料枠があります.

本記事では, Fly Postgres をベクトルデータベースとして使えるようにする手順を説明します.

まず docker image を作る

PostgreSQL をベクトルデータベースとして使うには, pgvector という拡張をインストールする必要があります.

そこで, Fly Postgres で使われている flyio/postgres-flex をベースイメージとし, pgvector をインストールしたカスタムイメージを作成します.

Dockerfile
ARG PG_MAJOR=15
FROM flyio/postgres-flex:${PG_MAJOR}

RUN apt-get update \
    && apt-get install -y --no-install-recommends postgresql-${PG_MAJOR}-pgvector \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# 以下はお好みで日本語用の設定
RUN echo "ja_JP.UTF-8 UTF-8" > /etc/locale.gen && locale-gen
ENV LANG ja_JP.UTF-8

ENV TZ Asia/Tokyo

docker image を build して, Docker レジストリに push します. 1

$ PG_MAJOR=15  # Postgres のメジャーバージョンを指定
$ docker image build . -t <your-name>/fly-pgvector:$PG_MAJOR --build-arg PG_MAJOR=$PG_MAJOR
$ docker image push <your-name>/fly-pgvector:$PG_MAJOR

Fly Postgres クラスタを立てる

push したカスタムイメージを使って, Fly Postgres クラスタを立てます. 2

$ fly pg create --image-ref <your-name>/fly-pgvector:$PG_MAJOR
automatically selected personal organization: <your-org>
Some regions require a paid plan (bom, fra, maa).
See https://fly.io/plans to set up a plan.

? Select region: Tokyo, Japan (nrt)
? Select configuration: Development - Single node, 1x shared CPU, 256MB RAM, 1GB disk
? Scale single node pg to zero after one hour? No
Creating postgres cluster in organization personal
Creating app...
Setting secrets on app <pg-app-name>...
Provisioning 1 of 1 machines with image <your-name>/fly-pgvector:$PG_MAJOR
Waiting for machine to start...
Machine 123456abcdef is created
==> Monitoring health checks
  Waiting for 123456abcdef to become healthy (started, 3/3)

Postgres cluster <pg-app-name> created
  Username:    postgres
  Password:    ********
  Hostname:    <pg-app-name>.internal
  Flycast:     *:*:*:*:*:*:*
  Proxy port:  5432
  Postgres port:  5433
  Connection string: postgres://postgres:********@<pg-app-name>.flycast:5432

Save your credentials in a secure place -- you won't be able to see them again!

Connect to postgres
Any app within the <your-org> organization can connect to this Postgres using the above connection string

Now that you've set up Postgres, here's what you need to understand: https://fly.io/docs/postgres/getting-started/what-you-should-know/

これで Postgres クラスタが立ち上がりました. 以下のコマンドでも確認できます.

$ fly pg ls
NAME            OWNER           STATUS          LATEST DEPLOY
<pg-app-name>   personal        deployed

ベクトルデータベース化する

まず Postgres データベースに接続します.

$ fly pg connect -a <pg-app-name>
Connecting to *:*:*:*:*:*:*:*... complete
psql (15.3 (Debian 15.3-1.pgdg110+1))
"help"でヘルプを表示します。

postgres=#

次に vector 拡張を有効化します.

postgres=# create extension vector;
CREATE EXTENSION

postgres=# select * from pg_extension where extname = 'vector';
  oid  | extname | extowner | extnamespace | extrelocatable | extversion | extconfig | extcondition
-------+---------+----------+--------------+----------------+------------+-----------+--------------
 16513 | vector  |       10 |         2200 | t              | 0.4.2      |           |
(1 )

これでベクトルデータベース化できました.

Embedding などするときの接続方法

ローカル環境へのポートフォワードは下記で可能です.

$ fly proxy 15432:5432 -a <pg-app-name>

localhost:15432 で接続できるようになります.

$ psql postgres://postgres:<password>@localhost:15432

インターネット経由で Postgres に接続したい場合は, 下記の公式ドキュメントに従って設定してください.

参考

LangChain で Postgres を使うときは, 下記の公式ドキュメントを参照.

  1. あらかじめ Docker Hub などのレジストリのアカウント作成や, docker login が必要です

  2. あらかじめ flyctl のインストールやサインイン が必要です

4
1
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
4
1