Install PostgreSQL with pgvector

PostgreSQL is a powerful database that we're using. Not only the common data, but the Vector data.

PostgreSQL supports vector data types by a extension called pgvector.

In this article, we're going to install PostgreSQL and pgvector.

Prerequisites

Assume you have a Linux system, and currently we are using CentOS 8.

And PostgreSQL 15 is the last version that supports CentOS 8. By the way, pgvector needs PostgreSQL 11 or later.

Install PostgreSQL

Go to the official site to download the rpm package.

Or you can just copy the following command to install it.

bash 复制代码
# Install the repository RPM:
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

# Install PostgreSQL:
yum install -y postgresql15-server

# Optionally initialize the database and enable automatic start:
/usr/pgsql-15/bin/postgresql-15-setup initdb
systemctl enable postgresql-15
systemctl start postgresql-15

Check the PostgreSQL if it's installed successfully.

bash 复制代码
psql --version

Some useful commands:

bash 复制代码
systemctl status postgresql-15
systemctl stop postgresql-15
systemctl restart postgresql-15

Setup PostgreSQL

After the installation, it will create a user named postgres automatically. Switch to the postgres user.

bash 复制代码
su - postgres

Now we enter the PostgreSQL shell, and change the password of the postgres user.

bash 复制代码
psql
sql 复制代码
ALTER USER postgres WITH PASSWORD 'postgres';

After completing these tasks, enter the exit command to quit.

Beware that the default password of Linux postgres user should be changed.

bash 复制代码
# 修改 linux 的 postgres 用户密码
# 注意,这个密码很容易被猜到,被攻击
 sudo  passwd -d postgres
 sudo -u postgres passwd

Install pgvector

By default, install pgvetor by compiling from source code. However, it's such a tough task with a lot of errors.

Eventually, we found a way to install pgvector by using the RPM package. It's SUPER easy and works well.

bash 复制代码
yum install pgvector_15

Create a user

Enter the PostgreSQL shell again: psql.

sql 复制代码
# 创建用户
CREATE USER dbroot WITH PASSWORD '123root';

# 创建数据库
CREATE DATABASE vectdb OWNER dbroot;

# 赋权
GRANT ALL PRIVILEGES ON DATABASE vectdb TO dbroot;

# 添加 superuser 权限,用于安装 pgvector 插件
alter user dbroot with superuser;

Then just create a vector table to test if it works.

sql 复制代码
# 本地登录
> psql -U dbroot -h localhost  -p 5432 -d vectdb

# 创建插件
CREATE EXTENSION vector;

# 创建一个带有向量类型的表
CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3));

# 写入一条数据
INSERT INTO items (embedding) VALUES ('[1,2,3]'), ('[4,5,6]');

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

Open PostgreSQL to the outside accessing

bash 复制代码
> cd /var/lib/pgsql/15/data/

> vim postgresql.conf
#将监听地址修改为*
#默认listen_addresses配置是注释掉的,所以可以直接在配置文件开头加入该行
listen_addresses='*'

> vim pg_hba.conf
#在文件尾部加入
#注意,开启所有外网都能访问时,服务容易被攻击
host  all  all 0.0.0.0/0 md5

#重启PostgreSQL服务
sudo systemctl restart postgresql-15

Some useful commands

bash 复制代码
#查看所有数据库
> \l

#切换当前数据库
> \c vectdb

#查看当前数据库下所有表
> \d

#退出数据库
> \q

# 查询配置文件所在位置
> show config_file;

# 查询数据储存目录
> show data_directory;

Thanks to these articles that helped me a lot!

相关推荐
心平愈三千疾2 小时前
通俗理解JVM细节-面试篇
java·jvm·数据库·面试
我科绝伦(Huanhuan Zhou)9 天前
Oracle|Oracle SQL*Plus 配置上下翻页功能
数据库·sql·oracle
Cachel wood9 天前
Spark教程6:Spark 底层执行原理详解
大数据·数据库·分布式·计算机网络·spark
java—大象9 天前
基于java SSM的房屋租赁系统设计和实现
java·开发语言·数据库·spring boot·layui·mybatis
Mutig_s9 天前
Spring Boot动态数据源切换:优雅实现多数据源管理
java·数据库·spring boot·后端·mybatis
Python小老六9 天前
单片机测ntc热敏电阻的几种方法(软件)
数据库·单片机·嵌入式硬件
矿渣渣9 天前
SQLite3 在嵌入式系统中的应用指南
数据库·sqlite·嵌入式实时数据库
@昵称不存在9 天前
Python csv 模块
开发语言·数据库·python
程序猿小D9 天前
[附源码+数据库+毕业论文]基于Spring+MyBatis+MySQL+Maven+Vue实现的校园二手交易平台管理系统,推荐!
java·数据库·mysql·spring·vue·毕业设计·校园二手交易平台
DoWeixin69 天前
【请关注】hBase要用的顺畅的思路
数据库