跳至主要內容

Postgres 擴充功能

總覽

Prisma Postgres 支援 PostgreSQL 擴充功能 (extensions),例如

請參閱下方的完整支援擴充功能清單

如果您希望在 Prisma Postgres 中看到特定的擴充功能,請填寫此表單

警告

Prisma Postgres 對 Postgres 擴充功能的支援目前處於搶先體驗 (Early Access) 階段,尚未建議用於生產環境。

在 Prisma ORM 中使用擴充功能

某些擴充功能可能已由 Prisma Postgres 支援,但尚未由 Prisma ORM 支援。Prisma ORM 即將針對部分 Postgres 擴充功能提供原生支援。在此期間,您仍然可以透過自訂遷移 (customized migrations)TypedSQL(或是其他在 Prisma ORM 中發送原生 SQL (raw SQL) 的機制)來使用這些擴充功能。

讓我們透過一個 pgvector 的範例來操作。

1. 建立一個空白的遷移檔案

若要自訂遷移,請先建立一個空白的遷移檔案

npx prisma migrate dev --name add-pgvector --create-only

請注意 --create-only 標誌,它會在您的 migrations 目錄中建立一個空白的遷移檔案。

2. 在您的遷移檔案中建立並使用擴充功能

在空白的遷移檔案中,您可以編寫任何要在資料庫中執行的自訂 SQL

-- prisma/migrations/<timestamp>-add-pgvector/migration.sql
CREATE EXTENSION IF NOT EXISTS vector;

CREATE TABLE "Document" (
id SERIAL PRIMARY KEY,
title TEXT NOT NULL,
embedding VECTOR(4) -- use 4 for demo purposes; real-world values are much bigger
);

在此範例中,您將:

  • 使用 CREATE EXTENSION 語句在資料庫中安裝 pgvector 擴充功能
  • 建立一個使用該擴充功能中 VECTOR 型別的 Document 資料表

3. 對資料庫執行遷移

執行以下命令以執行遷移並將變更套用到您的資料庫

npx prisma migrate deploy

此命令將套用待處理的 prisma/migrations/<timestamp>-add-pgvector/migration.sql 遷移,並在您的資料庫中建立 Document 資料表。

4. 將 document 資料表引入您的 Prisma Schema

使用新的 Document 資料表對資料庫 Schema 進行內省 (Introspect),並更新您的 Prisma Schema

npx prisma db pull
顯示CLI結果
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "postgres", schema "public" at "accelerate.prisma-data.net"

✔ Introspected 3 models and wrote them into prisma/schema.prisma in 3.23s

*** WARNING ***

These fields are not supported by Prisma Client, because Prisma currently does not support their types:
- Model: "Document", field: "embedding", original data type: "vector"

Run prisma generate to generate Prisma Client.

CLI 命令輸出中的警告是預期的,因為 Prisma ORM 尚未原生支援 VECTOR 型別。

您的 Prisma Schema 現在將包含 Document 模型

model Document {
id Int @id @default(autoincrement())
title String
embedding Unsupported("vector")?
}

由於 VECTOR 型別尚未由 Prisma ORM 原生支援,它將被表示為 Unsupported 型別。

5. 使用原生 SQL 進行查詢

以下是一個向 Document 資料表插入新列的查詢範例

await prisma.$executeRaw`
INSERT INTO "Document" (title, embedding)
VALUES ('My Title', '[1,22,1,42]'::vector)
`;

您也可以使用 TypedSQL 對您的資料庫進行型別安全的 SQL 查詢。

暫時性限制

擴充功能可用性有限

擴充功能支援適用於:

  • Pro 與 Business 方案中的所有執行個體 (instances)
  • 2025 年 8 月 12 日之後建立的 Free 與 Starter 方案中的所有執行個體

其餘執行個體很快也會獲得擴充功能支援。

如果您目前使用的執行個體不支援 PostgreSQL 擴充功能且您有相關需求,請聯繫我們尋求協助。

Prisma Studio 不支援來自擴充功能的特殊資料型別

Prisma Studio 目前不支援使用 PostgreSQL 擴充功能特殊型別的資料表。對於 pgvector,它將顯示類似以下的錯誤:

顯示 Prisma Studio 錯誤訊息
{
"error": "KnownError { message: \"Raw query failed. Code: `N/A`. Message: `Failed to deserialize column of type 'vector'. If you're using $queryRaw and this column is explicitly marked as `Unsupported` in your Prisma schema, try casting this column to any supported Prisma type such as `String`.`\", meta: Object {\"code\": String(\"N/A\"), \"message\": String(\"Failed to deserialize column of type 'vector'. If you're using $queryRaw and this column is explicitly marked as `Unsupported` in your Prisma schema, try casting this column to any supported Prisma type such as `String`.\")}, error_code: \"P2010\" }",
"user_facing_error": {
"is_panic": false,
"message": "Raw query failed. Code: `N/A`. Message: `Failed to deserialize column of type 'vector'. If you're using $queryRaw and this column is explicitly marked as `Unsupported` in your Prisma schema, try casting this column to any supported Prisma type such as `String`.`",
"meta": {
"code": "N/A",
"message": "Failed to deserialize column of type 'vector'. If you're using $queryRaw and this column is explicitly marked as `Unsupported` in your Prisma schema, try casting this column to any supported Prisma type such as `String`."
},
"error_code": "P2010"
}
}

所有支援的擴充功能

以下是完整清單與已修正的網址

其他擴充功能即將推出

以下擴充功能的支援即將推出:

如果您希望看到特定的擴充功能,請填寫此表單

© . This site is unofficial and not affiliated with Prisma Data, Inc.