Skip to content

非公式本サイトは非公式の日本語ドキュメントであり、Cloudflare 公式サイトではありません。最新情報はdevelopers.cloudflare.comをご確認ください。

Google Cloud SQL

Hyperdrive を Google Cloud SQL for Postgres のデータベースインスタンスに接続します。

最終更新 Markdown で表示Agent セットアップ

この例では、Hyperdrive を Google Cloud SQL の Postgres データベースインスタンスに接続する方法を説明します。

1. Hyperdrive のアクセスを許可する

Hyperdrive がデータベースに接続できるように、有効なユーザー認証情報とネットワークアクセスを用意します。

Cloud Console

Google Cloud Console でインスタンスを作成するとき、または既存のインスタンスを編集するとき:

Hyperdrive がインスタンスに到達できるようにするには:

  1. Cloud Console で、Hyperdrive を接続するインスタンスを選択します。
  2. 接続 > ネットワーキング を展開し、公開 IP が有効であることを確認して、ネットワークを追加 から 0.0.0.0/0 を入力します。
  3. 完了 > 保存 を選択して、変更を保存します。
  4. サイドバーで 概要 を選択し、インスタンスの 公開 IP アドレス を控えます。

Hyperdrive が接続するユーザーを作成するには:

  1. サイドバーで ユーザー を選択します。
  2. ユーザー アカウントを追加 を選択し、組み込み認証 を選択します。
  3. 名前(例: hyperdrive-user)を入力し、生成 を選択してパスワードを生成します。
  4. 追加 を選択してユーザーを作成する前に、このパスワードをクリップボードにコピーします。

ユーザー名、パスワード、公開 IP アドレス、(任意の)データベース名(デフォルト: postgres)が揃ったら、Hyperdrive のデータベース設定を作成できます。

gcloud CLI

gcloud CLI を使うと、新しいユーザーを作成し、Hyperdrive がデータベースに接続できるようにできます。

gcloud sql で、強力なパスワードを持つ新しいユーザー(例: hyperdrive-user)を作成します。

gcloud sql users create hyperdrive-user --instance=YOUR_INSTANCE_NAME --password=SUFFICIENTLY_LONG_PASSWORD

次のコマンドで、データベースインスタンスへの インターネットアクセス を有効にします。

# If you have any existing authorized networks, ensure you provide those as a comma separated list.
# The gcloud CLI will replace any existing authorized networks with the list you provide here.
gcloud sql instances patch YOUR_INSTANCE_NAME --authorized-networks="0.0.0.0/0"

追加の設定オプションは、Google Cloud のドキュメント を参照してください。

2. データベース設定を作成する

Hyperdrive を設定するには、次の情報が必要です。

  • データベースの IP アドレス(またはホスト名)とポート。
  • 前の手順で設定したデータベースのユーザー名(例: hyperdrive-demo)。
  • そのユーザー名に対応するパスワード。
  • Hyperdrive が接続するデータベース名。例: postgres

Hyperdrive は、これらのパラメーターを組み合わせた、データベースドライバーで一般的な接続文字列形式を受け付けます。

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

ほとんどのデータベースプロバイダーは、Hyperdrive にそのままコピー&ペーストできる接続文字列を提供します。

Cloudflare ダッシュボードで Hyperdrive 設定を作成する手順は次のとおりです。

  1. Cloudflare ダッシュボードで Hyperdrive ページを開きます。

    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-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>"

3. Worker から Hyperdrive を使う

node-postgres ドライバーをインストールします。

npm i pg@>8.16.3

TypeScript を使う場合は、型定義パッケージもインストールします。

npm i -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 });
		}
	},
};

次のステップ

役に立ちましたか?