加载中...
在Ubuntu22.04上部署postgresql&pgvector
发表于:2023-11-10 |

在Ubuntu22.04上部署postgresql&pgvector

至于为什么要pgvector这个插件,因为打算用华师的学生数据向量化对某llm进行prompt微调—–>chatECNU

1、安装Postgresql

1
2
sudo apt update
sudo apt install postgresql postgresql-contrib

默认用户postgres进入psql

1
2
sudo -u postgres psql
#退出 \q

通常,postgres用户仅仅在本地被使用。

2、创建 PostgreSQL 角色和数据库

仅仅超级用户和拥有CREATEROLE权限的角色可以创建新角色。

在下面的例子中,我们创建一个名称为john的角色,一个名称为johndb的数据库,并且授予数据库上的权限:

01.创建一个新的 PostgreSQL 角色:

1
sudo su - postgres -c "createuser testuser"

02.创建一个新的 PostgreSQL 数据库:

1
sudo su - postgres -c "createdb testdb"

想要授权用户操作数据库,连接到 PostgreSQL shell:

1
sudo -u postgres psql

并且运行下面的 query:

1
grant all privileges on database johndb to testuser;

3、启用远程访问 PostgreSQL 服务器

默认情况下,PostgreSQL 服务器仅仅监听本地网络接口:127.0.0.1

为了允许远程访问你的 PostgreSQL 服务器,打开配置文件postgresql.conf并且在CONNECTIONS AND AUTHENTICATION一节添加listen_addresses = '*'。我的配置文件所在目录etc/postgresql/14/main/postgresql.conf

1
2
3
4
5
6
7
8
sudo vim /etc/postgresql/14/main/postgresql.conf
#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------

# - Connection Settings -

listen_addresses = '*' # what IP address(es) to listen on;

保存文件并且重启 PostgreSQL 服务:

1
sudo service postgresql restart

使用ss工具验证修改:

1
ss -nlt | grep 5432

输出显示 PostgreSQL 服务器正在监听所有的网络接口(0.0.0.0):

1
2
LISTEN  0        244              0.0.0.0:5432           0.0.0.0:*              
LISTEN 0 244 [::]:5432 [::]:*

编辑pg_hba.conf文件。

1
2
# TYPE  DATABASE        USER            ADDRESS                 METHOD
host all testuser 0.0.0.0/0 md5

最后一步就是在你的防火墙上打开端口5432端口。

4、安装pgvector插件

1
2
3
4
5
cd /tmp
git clone --branch v0.5.1 https://github.com/pgvector/pgvector.git
cd pgvector
make
make install # may need sudo

如果在make时出现了postgres.h不存在的报错

For Ubuntu and Debian, use:

1
sudo apt install postgresql-server-dev-14

Note: Replace 15 with your Postgres server version 我的版本是14

Started

切换到指定数据库create这个插件\c testdb; 切换用户 \c - testuser;

1
2
3
4
5
6
7
8
9
10
11
root@thinkcentre:~/tmp/pgvector# sudo -u postgres psql
could not change directory to "/root/tmp/pgvector": 权限不够
psql (14.9 (Ubuntu 14.9-0ubuntu0.22.04.1))
Type "help" for help.

postgres=# \c testdb;
You are now connected to database "testdb" as user "postgres".
testdb=# CREATE EXTENSION vector;
CREATE EXTENSION
testdb=#

Docker部署postgresql + pgvector

注意:Navicat15 无法连接高于psotgresql 13+的版本,因此这里部署的均是13版本

原理是pgvector的仓库下有doc,在安装pgvector的Dockerfile中添加了编译pgvector的内容

1、docker build编译可用image

1
2
3
4
5
# git pgvector仓库
git clone --branch v0.4.4 https://github.com/pgvector/pgvector.git
cd pgvector
# docker编译13版本的postgresql
docker build --build-arg PG_MAJOR=13 -t myuser/pgvector .

2、部署container

1
docker run -d --name pgvector -e POSTGRES_USER=root -e POSTGRES_PASSWORD=789456 -p 5432:5432 myuser/pgvector:latest

3、进入容器新建用户和数据库

1
2
3
4
5
docker exec -it container_id psql -U root
# 创建用户
CREATE USER testuser WITH PASSWORD 'pwd';
# 创建数据库
CREATE DATABASE testdb OWNER testuser;

参考

https://zhuanlan.zhihu.com/p/143156636

https://github.com/pgvector/pgvector#installation-notes

https://duckcloud.cn/archives/1689163361465

上一篇:
Github Actions + Docker实现CI/CD
下一篇:
解决ssh登录linux主机后无法访问外网的问题