基于 pgvector 的 Docker 镜像,额外安装了 pg_jieba 中文分词插件。
当前版本:PostgreSQL 14
注意:由于 pg_jieba 扩展的兼容性限制,当前仅支持 PostgreSQL 14。未来版本将逐步支持 PostgreSQL 15+。
docker.cnb.cool/xbotter/pgvector-jieba:latest - 最新版本docker.cnb.cool/xbotter/pgvector-jieba:pg14 - PostgreSQL 14docker.cnb.cool/xbotter/pgvector-jieba:pg14-<commit> - 特定提交版本# 构建镜像
docker build -t pgvector-jieba:pg14 .
# 拉取最新版本
docker pull docker.cnb.cool/xbotter/pgvector-jieba:latest
# 拉取 PostgreSQL 14 版本
docker pull docker.cnb.cool/xbotter/pgvector-jieba:pg14
docker run -d \ --name postgres-pgvector-jieba \ -p 5432:5432 \ -e POSTGRES_PASSWORD=yourpassword \ -e POSTGRES_DB=mydb \ pgvector-jieba:pg14
docker exec -it postgres-pgvector-jieba psql -U postgres -d mydb
-- 测试中文分词
SELECT ts_debug('jiebacfg', '中文分词测试postgresql');
-- 创建测试表
CREATE TABLE documents (
id SERIAL PRIMARY KEY,
content TEXT
);
-- 插入测试数据
INSERT INTO documents (content) VALUES
('中文分词是自然语言处理的基础'),
('PostgreSQL 是功能强大的开源数据库');
-- 创建全文搜索索引
CREATE INDEX ON documents USING gin(to_tsvector('jiebacfg', content));
-- 全文搜索
SELECT * FROM documents
WHERE to_tsvector('jiebacfg', content) @@ to_tsquery('jiebacfg', '数据库');
-- 创建向量表
CREATE TABLE items (
id SERIAL PRIMARY KEY,
embedding vector(1536)
);
-- 插入向量数据
INSERT INTO items (embedding) VALUES ('[1,2,3]'), ('[4,5,6]');
-- 向量相似度搜索
SELECT * FROM items
ORDER BY embedding <-> '[3,1,2]'
LIMIT 5;
Dockerfile: 镜像构建文件init-pg-jieba.sql: 数据库初始化脚本,自动启用扩展pg_jieba.conf: pg_jieba 配置文件(可选)