0Pricing
Vector Databases: Pinecone, Weaviate & pgvector · บทเรียน

การจัดเก็บเวกเตอร์ใน PostgreSQL

เรียนรู้การสร้างตารางที่มีคอลัมน์เวกเตอร์ และแทรกข้อมูลเวกเตอร์ฝังตัวลงในฐานข้อมูล PostgreSQL โดยตรง

การจัดเก็บเวกเตอร์ใน PostgreSQL เป็นบทเรียน Vector Databases: Pinecone, Weaviate & pgvector ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Vector Databases: Pinecone, Weaviate & pgvector ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Vector Databases: Pinecone, Weaviate & pgvector มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การจัดเก็บเวกเตอร์ใน PostgreSQL”

เรียนรู้การสร้างตารางที่มีคอลัมน์เวกเตอร์ และแทรกข้อมูลเวกเตอร์ฝังตัวลงในฐานข้อมูล PostgreSQL โดยตรง คุณปฏิบัติ Vector Databases: Pinecone, Weaviate & pgvector ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Vector Databases: Pinecone, Weaviate & pgvector หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Vector Databases: Pinecone, Weaviate & pgvector บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 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