0Pricing
Vector Databases: Pinecone, Weaviate & pgvector · 课时

在 PostgreSQL 中存储向量

学习创建包含向量列的表,并将嵌入数据直接插入 PostgreSQL 数据库。

在 PostgreSQL 中存储向量 是 CoddyKit 上的免费 Vector Databases: Pinecone, Weaviate & pgvector 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Vector Databases: Pinecone, Weaviate & pgvector 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Vector Databases: Pinecone, Weaviate & pgvector 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Storing Vectors in PostgreSQL

Welcome back! In the previous lesson, we set up pgvector. Now, let's get practical and learn how to store actual vector embeddings in your PostgreSQL database.

This lesson covers creating tables with vector columns and inserting embedding data.

PostgreSQL Tables Reminder

Before storing vectors, let's quickly recall how tables work in PostgreSQL. You use the CREATE TABLE statement to define your table's structure, including column names and their data types.

With pgvector installed, we gain a new, powerful data type!

The `vector` Data Type

The core of storing embeddings with pgvector is the vector data type. When you define a column as vector(N), N represents the dimension of your embeddings.

  • vector(3): Stores 3-dimensional vectors.
  • vector(1536): Stores 1536-dimensional vectors (common for OpenAI embeddings).

It's crucial that N matches the dimension of the embeddings you're working with.

Creating a Table with Vectors

To store embeddings, you'll create a table just like any other, but specify one or more columns as type vector(N).

Here's an example creating a simple table to hold items and their 3-dimensional embeddings:

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

Understanding Vector Dimensions

The number N in vector(N) is vital. It must precisely match the number of values (dimensions) in your actual embedding vectors. If your embedding model produces 768-dimensional vectors, your column must be vector(768).

Mismatched dimensions will result in errors when inserting data. Always check your embedding model's output dimension!

Inserting Single Vector Data

Once your table is set up, you can insert vector data using the standard INSERT INTO SQL command. Vector values are provided as a string representation of a list of numbers, e.g., '[1.2, 3.4, 5.6]'.

INSERT INTO items (embedding)
VALUES ('[1.0, 2.0, 3.0]');

Inserting Multiple Vectors

You can insert multiple vectors into your table efficiently by providing multiple sets of values in a single INSERT INTO statement.

Each set of values corresponds to a new row in your table.

INSERT INTO items (embedding) VALUES
  ('[4.0, 5.0, 6.0]'),
  ('[7.0, 8.0, 9.0]');

Storing Vectors with Metadata

In real-world applications, you'll almost always store additional information (metadata) alongside your vectors. This could be text content, a user ID, a timestamp, or any other relevant data.

Just add more columns to your table definition:

CREATE TABLE documents (
  id SERIAL PRIMARY KEY,
  content TEXT,
  category VARCHAR(50),
  embedding vector(4)
);

Inserting Vectors & Metadata

When inserting data into a table with metadata, simply provide values for all the columns you wish to populate. Make sure the order of values matches the order of columns specified in your INSERT INTO statement.

INSERT INTO documents (content, category, embedding) VALUES
  ('The quick brown fox.', 'animal', '[0.1, 0.2, 0.3, 0.4]');

Quick Check: Table Creation

Which of the following SQL statements correctly creates a table named products with an auto-incrementing primary key id and a vector column product_embedding of 1536 dimensions?

Recap: Storing Your Vectors

Great job! You've learned the fundamental steps to store vector embeddings in PostgreSQL using pgvector:

  • Defining vector columns with vector(N), ensuring N matches your embedding dimensions.
  • Creating tables with both vector and metadata columns.
  • Inserting single and multiple vector data points, along with their associated metadata.

Next, we'll explore how to query this data to find similar vectors!

常见问题解答

「在 PostgreSQL 中存储向量」课时是免费的吗?

是的 — 「在 PostgreSQL 中存储向量」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Vector Databases: Pinecone, Weaviate & pgvector 课程的其余内容,请升级到 CoddyKit PRO。 Vector Databases: Pinecone, Weaviate & pgvector 课程共包含 4 节课。

「在 PostgreSQL 中存储向量」这节课中我会学到什么?

学习创建包含向量列的表,并将嵌入数据直接插入 PostgreSQL 数据库。 你通过在浏览器中直接运行的动手代码来练习 Vector Databases: Pinecone, Weaviate & pgvector,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Vector Databases: Pinecone, Weaviate & pgvector 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Vector Databases: Pinecone, Weaviate & pgvector 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「在 PostgreSQL 中存储向量」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Vector Databases: Pinecone, Weaviate & pgvector 课中编写并运行代码吗?

能。每节 Vector Databases: Pinecone, Weaviate & pgvector 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 设置 pgvector 扩展
  2. 在 PostgreSQL 中存储向量
  3. 执行相似度查询
  4. 在 pgvector 中选择距离指标
← 返回 Vector Databases: Pinecone, Weaviate & pgvector