Centos7 安装pg数据库

4 阅读2分钟

Centos7安装postgresql和pgvector向量数据库

centos7镜像仓库已经停止维护了而且如果是国内网络环境,推荐使用阿里云镜像安装,速度快。

1. 备份

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

2. 下载新的 CentOS-Base.repo 到 /etc/yum.repos.d/

CentOS 7

wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

或者

curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

3. 运行 yum makecache 生成缓存

我这里安装的pgsql15的版本

4.安装postgresql

4.1先添加pg数据库的镜像源
sudo tee /etc/yum.repos.d/pgdg-redhat-all.repo > /dev/null <<EOF
[pgdg15]
name=PostgreSQL 15 for RHEL 7 - Aliyun
baseurl=https://mirrors.aliyun.com/postgresql/repos/yum/15/redhat/rhel-7.9-x86_64
enabled=1
gpgcheck=0
EOF
4.2安装
# 清理缓存
yum clean all

# 生成缓存
yum makecache

# 安装 PostgreSQL 15
yum install -y postgresql15-server postgresql15
4.3 初始化数据库 + 设置密码

初始化数据库:

/usr/pgsql-15/bin/postgresql-15-setup initdb

启动服务:

systemctl enable postgresql-15
systemctl start postgresql-15

切换到 postgres 用户设置密码:

sudo -i -u postgres
psql

在 psql 里执行:

ALTER USER postgres WITH PASSWORD '你的安全密码';
\q

4.4设置可远程链接

  • 修改 postgresql.conf

    我这个文件在/var/lib/pgsql/15/data下

    如果你跟着我的来操作也极有可能在这个目录下

    或者是/etc/postgresql/

listen_addresses = '*'
  • 修改 pg_hba.conf
# IPv4 local connections:
host    all             all             0.0.0.0/0            scram-sha-256

5.安装pevector拓展

5.1需要先安装编译依赖
sudo yum install postgresql15-devel
5.2从github上拉取源码
git clone https://github.com/pgvector/pgvector.git

#进入源码目录

cd pgvector
执行指令
make
make install

如果出现了pg_config not found之类的先检查一下5.1指令是否成功completed

还是不对的话可能没有设置环境变量

找一下你的这个文件路径,我的是在 /usr/pgsql-15/bin/pg_config

export PATH=/usr/pgsql-15/bin:$PATH

然后测试:

pg_config --version

没问题了,之后就可以正常编译安装

5.3创建向量数据库

参考文档github

GitHub - pgvector/pgvector: Open-source vector similarity search for Postgres · GitHub

进入数据库

sudo -i -u postgres
psql

启用拓展

CREATE EXTENSION vector;

创建一个数据库

CREATE DATABASE knowledge;

进入数据库

\c knowledge

Create a vector column with 3 dimensions

CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3));

Insert vectors

INSERT INTO items (embedding) VALUES ('[1,2,3]'), ('[4,5,6]');

Get the nearest neighbors by L2 distance

SELECT * FROM items ORDER BY embedding <-> '[3,1,2]' LIMIT 5;

至此已经完成了postgresql数据库和插件pgvector向量插件的安装

参考文档地址:

https://www.cnblogs.com/yg_zhang/p/19067843

https://github.com/pgvector/pgvector

https://developer.aliyun.com/mirror/centos?spm=a2c6h.13651102.0.0.1a0f1b11q3KUnN