Postgres 扩展
概述
Prisma Postgres 支持 PostgreSQL 扩展,例如:
有关支持的扩展的完整列表,请参见下文。
如果您希望在 Prisma Postgres 中看到特定的扩展,请填写此表单。
Prisma Postgres 中的 Postgres 扩展支持目前处于 早期访问阶段,暂不建议用于生产环境。
将扩展与 Prisma ORM 结合使用
某些扩展可能已被 Prisma Postgres 支持,但尚未被 Prisma ORM 支持。Prisma ORM 对某些 Postgres 扩展的原生支持即将推出。在此期间,您仍然可以使用 自定义迁移和 TypedSQL(或通过 Prisma ORM 发送原始 SQL 的其他机制)来使用这些扩展。
我们来看一个使用 pgvector 的示例。
1. 创建一个空的迁移文件
要自定义迁移,首先创建一个空的迁移文件
npx prisma migrate dev --name add-pgvector --create-only
请注意 --create-only 标志,它将在您的迁移目录中创建一个空的迁移文件。
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. 将文档表拉入您的 Prisma schema
内省具有新 Document 表的数据库 schema 并使用它更新您的 Prisma schema
npx prisma db pull
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 类型。
4. 使用原始 SQL 查询
以下是一个插入新行到 Document 表的示例查询
await prisma.$executeRaw`
INSERT INTO "Document" (title, embedding)
VALUES ('My Title', '[1,22,1,42]'::vector)
`;
您还可以使用 TypedSQL 对数据库执行类型安全的 SQL 查询。
临时限制
扩展可用性有限
扩展支持
- 专业版和商业版计划下的所有实例
- 2025 年 8 月 12 日之后创建的免费版和入门版计划下的所有实例
其余实例将很快获得扩展支持。
如果您当前使用的实例不支持 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"
}
}
所有支持的扩展
这是带有更正 URL 的完整列表
amcheckautoincbloombtree_ginbtree_gistcitextcubedblinkdict_intdict_xsynearthdistancefile_fdwfuzzystrmatchhstoreinsert_usernameintaggintarrayisnloltreemoddatetimepageinspectpg_buffercachepg_freespacemappg_prewarmpg_searchpg_stat_statementspg_surgerypg_trgmpg_visibilitypg_walinspectpgcryptopgrowlockspgstattupleplpgsqlpostgres_fdwrefintsegsslinfotablefunctcntsm_system_rowstsm_system_timeunaccentuuid-osspvectorxml2
其他扩展即将推出
对以下扩展的支持即将推出
如果您希望看到特定的扩展,请填写此表单。