PostgreSQL 向量扩展插件pgvector安装和使用

文章目录

PostgreSQL 向量扩展插件pgvector安装和使用

pgvector 是一个 PostgreSQL 扩展,能够让PostgreSQL 提供向量(vector)数据类型和相关操作功能。

参考:
https://github.com/pgvector/pgvector

安装postgresql

利用pgvector 前提是事先安装好postgresql,安装postgresql例可参考下文。

参考:
Linux(Ubuntu)源码安装postgresql16.3
https://editor.csdn.net/md/articleId=142500497

pgvector下载和安装

安装命令如下:

cd 
git clone --branch v0.7.4 https://github.com/pgvector/pgvector.git
cd pgvector
make
make install # may need sudo

参考:
https://github.com/pgvector/pgvector

例:下载

root@autodl-container-616f40a3b3-41cb82d9:~#  su - postgres
postgres@autodl-container-616f40a3b3-41cb82d9:~$ pwd
/var/lib/postgresql
postgres@autodl-container-616f40a3b3-41cb82d9:~$ ls
10  pgsql16  postgresql-16.3  postgresql-16.3.tar.gz
postgres@autodl-container-616f40a3b3-41cb82d9:~$ git clone --branch v0.7.4 https://github.com/pgvector/pgvector.git
Cloning into 'pgvector'...
remote: Enumerating objects: 10650, done.
remote: Counting objects: 100% (4165/4165), done.
remote: Compressing objects: 100% (954/954), done.
remote: Total 10650 (delta 3388), reused 3493 (delta 3193), pack-reused 6485 (from 1)
Receiving objects: 100% (10650/10650), 1.60 MiB | 544.00 KiB/s, done.
Resolving deltas: 100% (7952/7952), done.
Note: checking out '103ac50f1a90b47a72003e8e8628a55ec372f202'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

postgres@autodl-container-616f40a3b3-41cb82d9:~$
安装错误调试
错误调试1

尝试编译和安装
例:

postgres@autodl-container-616f40a3b3-41cb82d9:~/pgvector$ make
You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application.
cp sql/vector.sql sql/vector--0.7.4.sql

编译时候汇报如上错误可能是由于postgresql没有启动。

postgres@autodl-container-616f40a3b3-41cb82d9:~/pgvector$ psql
psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: Connection refused
        Is the server running locally and accepting connections on that socket?
postgres@autodl-container-616f40a3b3-41cb82d9:~/pgvector$ 
postgres@autodl-container-616f40a3b3-41cb82d9:~$ ps -xf|grep postgres
  1504 pts/3    S+     0:00  _ grep postgres
postgres@autodl-container-616f40a3b3-41cb82d9:~$ pg_ctl -D /var/lib/postgresql/pgsql16/data status
pg_ctl: no server running
尝试解决 AP1 :启动postgresql
postgres@autodl-container-616f40a3b3-41cb82d9:~$ pg_ctl -D /var/lib/postgresql/pgsql16/data -l logfile start
pg_ctl: another server might be running; trying to start server anyway
waiting for server to start.... done
server started
postgres@autodl-container-616f40a3b3-41cb82d9:~$  ps -xf|grep postgres
  1197 pts/0    S+     0:00  _ grep postgres
  1188 ?        Ss     0:00 /var/lib/postgresql/pgsql16/bin/postgres -D /var/lib/postgresql/pgsql16/data
  1189 ?        Ss     0:00  _ postgres: checkpointer 
  1190 ?        Ss     0:00  _ postgres: background writer 
  1192 ?        Ss     0:00  _ postgres: walwriter 
  1193 ?        Ss     0:00  _ postgres: autovacuum launcher 
  1194 ?        Ss     0:00  _ postgres: logical replication launcher 
postgres@autodl-container-616f40a3b3-41cb82d9:~$  psql
psql (16.3)
Type "help" for help.

postgres=# help
You are using psql, the command-line interface to PostgreSQL.
Type:  copyright for distribution terms
       h for help with SQL commands
       ? for help with psql commands
       g or terminate with semicolon to execute query
       q to quit
postgres=# 
错误调试2

再次编译,出错。

postgres@autodl-container-616f40a3b3-41cb82d9:~$ pwd
/var/lib/postgresql
postgres@autodl-container-616f40a3b3-41cb82d9:~$ cd pgvector
postgres@autodl-container-616f40a3b3-41cb82d9:~/pgvector$ 
postgres@autodl-container-616f40a3b3-41cb82d9:~/pgvector$ make
You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-si
### 在 Windows 上为 PostgreSQL 向量数据库安装 pgvector 插件 要在 Windows 平台上为 PostgreSQL 数据库安装 `pgvector` 插件,需遵循一系列特定步骤来完成环境准备、插件下载以及编译过程。以下是详细的说明: #### 环境需求 确保已满足以下条件: - 已安装 PostgreSQL 14 或更高版本[^3]。 - 安装 Visual Studio 2022 或更新版本,因为该工具链支持 C++ 的构建环境,这对于编译 `pgvector` 所需的源代码至关重要。 #### 步骤概述 ##### 下载源码 通过 Git 克隆 `pgvector` 的官方仓库至本地计算机。命令如下所示: ```bash git clone --branch v0.6.2 https://github.com/pgvector/pgvector.git ``` ##### 配置开发环境 进入克隆后的项目文件夹,并设置必要的环境变量以便于后续编译工作顺利开展。具体操作包括调整 PATH 变量以包含 PostgreSQL Microsoft Build Tools 的路径。 ##### 编译与安装 利用 MSVC(Microsoft Visual C++)进行编译处理。打开项目的解决方案文件 (.sln),选择适合目标平台架构(通常是 x64),然后启动构建流程。成功完成后,在指定输出目录下会生成 `.dll` 文件作为最终产物之一。 最后一步是在 PostgreSQL 实例中注册新创建好的扩展模块。连接到对应的数据库实例之后运行 SQL 命令加载 extension 即可生效: ```sql CREATE EXTENSION vector; ``` 以上即完成了整个在 Windows 操作系统之上针对 PostgreSQL 使用向量相似度搜索功能所需做的准备工作。 ### 注意事项 由于不同用户的实际部署情况可能存在差异,请务必参照各自系统的具体情况作出适当调整;另外考虑到兼容性稳定性因素建议始终采用最新稳定发布的软件组件版本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值