この例では、Hyperdrive を Timescale ↗ の時系列データベースに接続する方法を示します。Timescale は PostgreSQL 上に構築されており、時系列、イベント、分析の強力な機能を備えています。
Timescale の詳細は、Timescale services のドキュメント ↗ を参照してください。
既存の Timescale データベースには、新しいユーザーを作成し、データベースの接続文字列を取得することで接続できます。
Timescale Console ↗ で認証情報とデータベースエンドポイントを取得する手順は次のとおりです。
- Hyperdrive を接続するサービス(データベース)を選択します。
- Connection info を展開します。
- Service URL をコピーします。Service URL は、Hyperdrive が接続に使う接続文字列です。この文字列には、データベースのホスト名、ポート番号、データベース名が含まれます。
パスワードを保管していない場合は、Forgot your password? を選択し、新しい SCRAM パスワードを設定します。Timescale はこのパスワードを一度しか表示しないため、保存してください。
接続文字列は、次のような形式になります。
postgres://tsdbadmin:YOUR_PASSWORD_HERE@pn79dztyy0.xzhhbfensb.tsdb.cloud.timescale.com:31358/tsdb接続文字列があれば、Hyperdrive のデータベース設定を作成できます。
Hyperdrive を設定するには、次の情報が必要です。
- データベースの IP アドレス(またはホスト名)とポート。
- 前の手順で設定したデータベースのユーザー名(例:
hyperdrive-demo)。 - そのユーザー名に対応するパスワード。
- Hyperdrive が接続するデータベース名。例:
postgres。
Hyperdrive は、これらのパラメーターを組み合わせた、データベースドライバーで一般的な接続文字列形式を受け付けます。
postgres://USERNAME:PASSWORD@HOSTNAME_OR_IP_ADDRESS:PORT/database_nameほとんどのデータベースプロバイダーは、Hyperdrive にそのままコピー&ペーストできる接続文字列を提供します。
Cloudflare ダッシュボードで Hyperdrive 設定を作成する手順は次のとおりです。
-
Cloudflare ダッシュボードで Hyperdrive ページを開きます。
Hyperdrive を開く ↗ -
Create Configuration を選択します。
-
接続文字列を含め、フォームに入力します。
-
Create を選択します。
Wrangler CLI で Hyperdrive 設定を作成する手順は次のとおりです。
-
ターミナルを開き、次のコマンドを実行します。
<NAME_OF_HYPERDRIVE_CONFIG>を Hyperdrive 設定の名前に置き換え、データベースホストから提供された接続文字列を貼り付けるか、user、password、HOSTNAME_OR_IP_ADDRESS、port、database_nameのプレースホルダーをデータベース固有の値に置き換えます。npx wrangler hyperdrive create <NAME_OF_HYPERDRIVE_CONFIG> --connection-string="postgres://user:password@HOSTNAME_OR_IP_ADDRESS:PORT/database_name" -
このコマンドは、Wrangler 設定ファイル 向けのバインディングを出力します。
{ "$schema": "./node_modules/wrangler/config-schema.json", "name": "hyperdrive-example", "main": "src/index.ts", // Set this to today's date "compatibility_date": "2026-09-20", "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-09-20" compatibility_flags = [ "nodejs_compat" ] [[hyperdrive]] binding = "HYPERDRIVE" id = "<ID OF THE CREATED HYPERDRIVE CONFIGURATION>"
node-postgres ドライバーをインストールします。
npm i pg@>8.16.3yarn add pg@>8.16.3pnpm add pg@>8.16.3bun add pg@>8.16.3TypeScript を使う場合は、型定義パッケージもインストールします。
npm i -D @types/pgyarn add -D @types/pgpnpm add -D @types/pgbun add -d @types/pg必要な Node.js 互換性フラグと Hyperdrive バインディングを、wrangler.jsonc ファイルに追加します。
{
// required for database drivers to function
"compatibility_flags": [
"nodejs_compat"
],
// Set this to today's date
"compatibility_date": "2026-09-20",
"hyperdrive": [
{
"binding": "HYPERDRIVE",
"id": "<your-hyperdrive-id-here>"
}
]
}compatibility_flags = [ "nodejs_compat" ]
# Set this to today's date
compatibility_date = "2026-09-20"
[[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 });
}
},
};- Hyperdrive の仕組み について詳しく学びます。
- よくある問題の切り分けは トラブルシューティングガイド を参照してください。
- Cloudflare Workers で使えるほかの ストレージオプション についても確認します。