跳转到内容
搜索文档

Prisma Postgres 示例

将 Hyperdrive 连接到 Prisma Postgres 数据库。

最后更新 查看 MarkdownAgent 设置

本示例展示如何将 Hyperdrive 连接到 Prisma Postgres 数据库。

1. 允许 Hyperdrive 访问

可使用现有数据库连接字符串,将 Hyperdrive 连接到任何现有 Prisma Postgres 数据库。

Prisma 数据平台

  1. 前往 Prisma Data Platform Console 并选择要连接的项目(数据库)。
  2. 选择 Connect to your database(连接到数据库) > Any client(任意客户端)
  3. 选择 Generate database credentials(生成数据库凭据)。复制 Prisma Postgres 数据库的连接字符串。
  4. 编辑连接字符串使其与 Hyperdrive 兼容。
  • 在端口后添加数据库名称。可移除任何查询参数,例如 ?sslmode=require
  • 最终字符串如下:
postgres://USERNAME:PASSWORD@HOSTNAME_OR_IP_ADDRESS:PORT/database_name

有了此连接字符串后,即可创建 Hyperdrive 数据库配置。

2. 创建数据库配置

配置 Hyperdrive 需要:

  • 数据库的 IP 地址(或主机名)和端口。
  • 上一步配置的数据库用户名(例如 hyperdrive-demo)。
  • 该用户名对应的密码。
  • 希望 Hyperdrive 连接的数据库名称,例如 postgres

Hyperdrive 接受数据库驱动常用的连接字符串格式组合上述参数:

postgres://USERNAME:PASSWORD@HOSTNAME_OR_IP_ADDRESS:PORT/database_name

大多数数据库提供商会提供可直接复制到 Hyperdrive 的连接字符串。

在 Cloudflare 仪表板中创建 Hyperdrive 配置:

  1. 在 Cloudflare 仪表板中,前往 Hyperdrive 页面。

    Go to Hyperdrive ↗
  2. 选择 Create Configuration(创建配置)

  3. 填写表单,包括连接字符串。

  4. 选择 Create(创建)

使用 Wrangler CLI 创建 Hyperdrive 配置:

  1. 打开终端并运行以下命令。将 <NAME_OF_HYPERDRIVE_CONFIG> 替换为 Hyperdrive 配置名称,并粘贴数据库主机提供的连接字符串,或将 userpasswordHOSTNAME_OR_IP_ADDRESSportdatabase_name 占位符替换为你的数据库信息:

    npx wrangler hyperdrive create <NAME_OF_HYPERDRIVE_CONFIG> --connection-string="postgres://user:password@HOSTNAME_OR_IP_ADDRESS:PORT/database_name"
  2. 该命令会输出 Wrangler 配置文件 的绑定:

    {
    	"$schema": "./node_modules/wrangler/config-schema.json",
    	"name": "hyperdrive-example",
    	"main": "src/index.ts",
    	// Set this to today's date
    	"compatibility_date": "2026-08-17",
    	"compatibility_flags": [
    		"nodejs_compat"
    	],
    	// Pasted from the output of `wrangler hyperdrive create <NAME_OF_HYPERDRIVE_CONFIG> --connection-string=[...]` above.
    	"hyperdrive": [
    		{
    			"binding": "HYPERDRIVE",
    			"id": "<ID OF THE CREATED HYPERDRIVE CONFIGURATION>"
    		}
    	]
    }
    "$schema" = "./node_modules/wrangler/config-schema.json"
    name = "hyperdrive-example"
    main = "src/index.ts"
    # Set this to today's date
    compatibility_date = "2026-08-17"
    compatibility_flags = [ "nodejs_compat" ]
    
    [[hyperdrive]]
    binding = "HYPERDRIVE"
    id = "<ID OF THE CREATED HYPERDRIVE CONFIGURATION>"

3. 从 Worker 使用 Hyperdrive

安装 node-postgres 驱动:

npm i pg@>8.16.3

若使用 TypeScript,安装类型包:

npm i -D @types/pg

wrangler.jsonc 中添加所需的 Node.js 兼容性标志和 Hyperdrive 绑定:

wrangler.jsonc 中添加 Node.js 兼容性标志和 Hyperdrive 绑定(binding):

{
	// required for database drivers to function
	"compatibility_flags": [
		"nodejs_compat"
	],
	// Set this to today's date
	"compatibility_date": "2026-08-17",
	"hyperdrive": [
		{
			"binding": "HYPERDRIVE",
			"id": "<your-hyperdrive-id-here>"
		}
	]
}
compatibility_flags = [ "nodejs_compat" ]
# Set this to today's date
compatibility_date = "2026-08-17"

[[hyperdrive]]
binding = "HYPERDRIVE"
id = "<your-hyperdrive-id-here>"

创建新的 Client 实例并传入 Hyperdrive connectionString

// filepath: src/index.ts
import { Client } from "pg";

export default {
	async fetch(
		request: Request,
		env: Env,
		ctx: ExecutionContext,
	): Promise<Response> {
		// Create a new client instance for each request. Hyperdrive maintains the
		// underlying database connection pool, so creating a new client is fast.
		const client = new Client({
			connectionString: env.HYPERDRIVE.connectionString,
		});

		try {
			// Connect to the database
			await client.connect();

			// Perform a simple query
			const result = await client.query("SELECT * FROM pg_tables");

			return Response.json({
				success: true,
				result: result.rows,
			});
		} catch (error: any) {
			console.error("Database error:", error.message);

			return new Response("Internal error occurred", { status: 500 });
		}
	},
};

4. 配置 Hyperdrive 最大连接数

Prisma Postgres 对使用 Hyperdrive 进行直接连接的数量有限制。请参阅 Prisma Postgres 限制

  1. Cloudflare Hyperdrive 仪表板 中,选择新创建的 Hyperdrive 配置。
  2. 前往 Settings(设置)
  3. Origin connection limit(源站连接限制) 中,选择 Edit Settings(编辑设置),将最大连接数设为低于 Prisma 连接限制的值。
  1. 使用 --origin-connection-limit 参数编辑现有 Hyperdrive 配置:

    npx wrangler hyperdrive update <HYPERDRIVE_ID> --origin-connection-limit=10

    <HYPERDRIVE_ID> 替换为你的 Hyperdrive 配置 ID,并将连接限制设为低于 Prisma 连接限制的值。

  2. 验证配置更改:

    npx wrangler hyperdrive get <HYPERDRIVE_ID>

后续步骤

这篇文档对您有帮助吗?