Skip to Content
リファレンスストレージPostgreSQL ストレージ

PostgreSQLストレージ

PostgreSQLストレージ実装は、PostgreSQLデータベースを使用した本番環境対応のストレージソリューションを提供します。

インストール

npm install @mastra/pg@latest

使い方

import { PostgresStore } from "@mastra/pg"; const storage = new PostgresStore({ connectionString: process.env.DATABASE_URL, });

パラメーター

connectionString:

string
PostgreSQL の接続文字列(例: postgresql://user:pass@host:5432/dbname)

schemaName?:

string
ストレージで使用したいスキーマ名。指定しない場合はデフォルトのスキーマが使用されます。

コンストラクタの例

PostgresStore は以下の方法でインスタンス化できます。

import { PostgresStore } from "@mastra/pg"; // 接続文字列のみを使用する場合 const store1 = new PostgresStore({ connectionString: "postgresql://user:password@localhost:5432/mydb", }); // 接続文字列とカスタムスキーマ名を使用する場合 const store2 = new PostgresStore({ connectionString: "postgresql://user:password@localhost:5432/mydb", schemaName: "custom_schema", // オプション }); // 個別の接続パラメータを使用する場合 const store4 = new PostgresStore({ host: "localhost", port: 5432, database: "mydb", user: "user", password: "password", }); // 個別パラメータと schemaName を使用する場合 const store5 = new PostgresStore({ host: "localhost", port: 5432, database: "mydb", user: "user", password: "password", schemaName: "custom_schema", // オプション });

追加の注意事項

スキーマ管理

ストレージ実装はスキーマの作成と更新を自動的に処理します。以下のテーブルが作成されます:

  • threads: 会話スレッドを保存します
  • messages: 個々のメッセージを保存します
  • metadata: スレッドやメッセージの追加メタデータを保存します