0
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?

Rocky Linux 9にPostgreSQL 16+pgvectorをインストールする

Posted at

What's?

PostgreSQL 16にpgvectorをインストールしてみよう、ということで。

pgvector

pgvectorは、ベクトル検索ができるPostgreSQLのextensionです。

PostgreSQL 13以上で使えるようです。

  • 正確なkNN、ANN
  • 単精度浮動小数点、半精度浮動小数点、バイナリ、 疎ベクトルをサポート
  • ユークリッド距離、内積、コサイン距離、マンハッタン距離、ハミング距離、ジャッカード距離
  • 各言語向けのクライアントがある

現時点でのバージョンは0.8.0です。

今回はインストールして簡単に動かして終了しようと思うのですが、インストール方法もたくさんあってよいですね。

pgvector / Additional Installation Methods

クライアントの一覧はこちら。

pgvector / Languages

ローカルでよく使われそうなものは、DockerHubのイメージかなと思います。

各種バージョンのPostgreSQLとの組み合わせでタグがあります。

OSのパッケージマネージャーでインストールする場合は、PostgreSQLの提供するリポジトリからpgvectorもインストールできるようなので、今回はRocky Linux 9で試してみようと思います。

このパターンに当てはまるのは、aptとdnf(yum)ですね。

環境

今回の環境はこちら。Rocky Linux 9です。

$ cat /etc/redhat-release
Rocky Linux release 9.5 (Blue Onyx)


$ uname -srvmpio
Linux 5.14.0-503.26.1.el9_5.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Feb 19 16:28:19 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux

PostgreSQL 16+pgvectorをインストールする

まずは、こちらに習ってRocky Linux 9にPostgreSQL 16をインストールします。

PostgreSQLのインストールまで。

$ sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
$ sudo dnf -qy module disable postgresql
$ sudo dnf install -y postgresql16-server

PostgreSQL 16.8がインストールされました。

$ psql --version
psql (PostgreSQL) 16.8

データベースクラスタの作成。

$ sudo PGSETUP_INITDB_OPTIONS='--encoding=UTF-8 --no-locale' postgresql-16-setup initdb

ロケールの確認。

$ sudo grep lc_ /var/lib/pgsql/16/data/postgresql.conf
lc_messages = C                         # locale for system error message
lc_monetary = C                         # locale for monetary formatting
lc_numeric = C                          # locale for number formatting
lc_time = C                             # locale for time formatting

では、pgvectorをインストールしてみます。

パッケージの検索。

$ dnf search pgvector
メタデータの期限切れの最終確認: 0:00:10 前の 2025年02月24日 21時37分00秒 に実施しました。
========================================================================= 名前 & 概要 一致: pgvector ==========================================================================
pgvector_12-llvmjit.x86_64 : Just-in-time compilation support for pgvector
pgvector_13-llvmjit.x86_64 : Just-in-time compilation support for pgvector
pgvector_14-llvmjit.x86_64 : Just-in-time compilation support for pgvector
pgvector_15-llvmjit.x86_64 : Just-in-time compilation support for pgvector
pgvector_16-llvmjit.x86_64 : Just-in-time compilation support for pgvector
pgvector_17-llvmjit.x86_64 : Just-in-time compilation support for pgvector
============================================================================= 名前 一致: pgvector =============================================================================
pgvector_12.x86_64 : Open-source vector similarity search for Postgres
pgvector_13.x86_64 : Open-source vector similarity search for Postgres
pgvector_14.x86_64 : Open-source vector similarity search for Postgres
pgvector_15.x86_64 : Open-source vector similarity search for Postgres
pgvector_16.x86_64 : Open-source vector similarity search for Postgres
pgvector_17.x86_64 : Open-source vector similarity search for Postgres

確かにインストールできそうです。

パッケージの情報。

$ dnf info pgvector_16
メタデータの期限切れの最終確認: 0:01:09 前の 2025年02月24日 21時37分00秒 に実施しました。
利用可能なパッケージ
名前         : pgvector_16
バージョン   : 0.8.0
リリース     : 1PGDG.rhel9
Arch         : x86_64
サイズ       : 107 k
ソース       : pgvector_16-0.8.0-1PGDG.rhel9.src.rpm
リポジトリー : pgdg16
概要         : Open-source vector similarity search for Postgres
URL          : https://github.com/pgvector/pgvector/
ライセンス   : PostgreSQL
説明         : Open-source vector similarity search for Postgres. Supports L2 distance,
             : inner product, and cosine distance

インストール。

$ sudo dnf install pgvector_16

PostgreSQLを起動して使ってみます。

$ sudo systemctl enable postgresql-16
$ sudo systemctl start postgresql-16

接続。

$ sudo -u postgres psql

この時点ではpgvectorは認識していません。

postgres=# \dx
                    インストール済みの拡張一覧
  名前   | バージョン |  スキーマ  |             説明
---------+------------+------------+------------------------------
 plpgsql | 1.0        | pg_catalog | PL/pgSQL procedural language
(1 )

ひとまず、データベースとユーザーを作成します。

postgres=# create user myuser password 'password';
CREATE ROLE
postgres=# create database example owner myuser;
CREATE DATABASE

データベースの切り替え。

postgres=# \c example
データベース"example"にユーザー"postgres"として接続しました。

ここからはpgvectorのGetting Startedに沿って進めます。

pgvector / Getting Started

pgvectorの導入。

example=# create extension vector;
CREATE EXTENSION

今度は認識されました。

example=# \dx
                                インストール済みの拡張一覧
  名前   | バージョン |  スキーマ  |                         説明
---------+------------+------------+------------------------------------------------------
 plpgsql | 1.0        | pg_catalog | PL/pgSQL procedural language
 vector  | 0.8.0      | public     | vector data type and ivfflat and hnsw access methods
(2 )

作成したユーザーでログインしなおします。

$ sudo -u postgres psql -U myuser -h localhost example
ユーザー myuser のパスワード:
psql (16.8)
"help"でヘルプを表示します。

example=>

テーブルの作成。

example=> create table items (id bigserial primary key, embedding vector(3));
CREATE TABLE

データの登録。

example=> insert into items (embedding) values ('[1,2,3]'), ('[4,5,6]');
INSERT 0 2

マンハッタン距離でorder by

example=> select * from items order by embedding <-> '[3,1,2]' limit 5;
 id | embedding
----+-----------
  1 | [1,2,3]
  2 | [4,5,6]
(2 )

ひとまず、導入というk

0
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
0
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?