> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Google Cloud SQL for PostgreSQL 集成

> 使用 LangChain JavaScript 集成 Google Cloud SQL for PostgreSQL 文档加载器。

[Cloud SQL](https://cloud.google.com/sql) 是一项完全托管的关系型数据库服务，提供高性能、无缝集成以及出色的可扩展性，并支持 PostgreSQL 等数据库引擎。

本指南简要介绍如何使用 Cloud SQL for PostgreSQL 通过 `PostgresLoader` 类加载文档。

## 概述

### 开始之前

要使用此软件包，您首先需要完成以下步骤：

1. [选择或创建一个 Cloud Platform 项目。](https://developers.google.com/workspace/guides/create-project)
2. [为您的项目启用计费功能。](https://cloud.google.com/billing/docs/how-to/modify-project#enable_billing_for_a_project)
3. [启用 Cloud SQL Admin API。](https://console.cloud.google.com/flows/enableapi?apiid=sqladmin.googleapis.com)
4. [设置身份验证。](https://cloud.google.com/docs/authentication)
5. [创建一个 CloudSQL 实例](https://cloud.google.com/sql/docs/postgres/connect-instance-auth-proxy#create-instance)
6. [创建一个 CloudSQL 数据库](https://cloud.google.com/sql/docs/postgres/create-manage-databases)
7. [向数据库添加用户](https://cloud.google.com/sql/docs/postgres/create-manage-users)

### 身份验证

使用 `gcloud auth login` 命令在本地对您的 Google Cloud 帐户进行身份验证。

### 设置您的 Google Cloud 项目

设置您的 Google Cloud 项目 ID，以便在本地利用 Google Cloud 资源：

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
gcloud config set project YOUR-PROJECT-ID
```

如果您不知道项目 ID，请尝试以下操作：

* 运行 `gcloud config list`。
* 运行 `gcloud projects list`。
* 查看支持页面：[查找项目 ID](https://support.google.com/googleapi/answer/7014113)。

## 设置 PostgresLoader 实例

要使用 PostgresLoader 类，您需要安装 `@langchain/google-cloud-sql-pg` 软件包，然后按照以下步骤操作。

首先，您需要登录您的 Google Cloud 帐户，并根据您的 Google Cloud 项目设置以下环境变量；这些变量将根据您配置 PostgresEngine 实例的方式（fromInstance、fromEngine、fromEngineArgs）来定义：

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
PROJECT_ID="your-project-id"
REGION="your-project-region" // 示例："us-central1"
INSTANCE_NAME="your-instance"
DB_NAME="your-database-name"
DB_USER="your-database-user"
PASSWORD="your-database-password"
```

### 设置实例

要实例化 PostgresLoader，您首先需要通过 PostgresEngine 创建一个数据库连接。

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import {
  PostgresLoader,
  PostgresEngine,
  PostgresEngineArgs,
} from "@langchain/google-cloud-sql-pg";
import * as dotenv from "dotenv";

dotenv.config();

const peArgs: PostgresEngineArgs = {
  user: process.env.DB_USER ?? "",
  password: process.env.PASSWORD ?? "",
};

// PostgresEngine 实例化
const engine: PostgresEngine = await PostgresEngine.fromInstance(
  process.env.PROJECT_ID ?? "",
  process.env.REGION ?? "",
  process.env.INSTANCE_NAME ?? "",
  process.env.DB_NAME ?? "",
  peArgs
);
```

### 使用 table\_name 参数加载文档

加载器使用第一列作为 page\_content，所有其他列作为元数据，从表中返回文档列表。默认表将第一列作为 page\_content，第二列作为元数据（JSON）。每一行成为一个文档。

```js theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
const documentLoaderArgs: PostgresLoaderOptions = {
  tableName: "test_table_custom",
  contentColumns: ["fruit_name", "variety"],
  metadataColumns: [
    "fruit_id",
    "quantity_in_stock",
    "price_per_unit",
    "organic",
  ],
  format: "text",
};

const documentLoaderInstance = await PostgresLoader.initialize(
  PEInstance,
  documentLoaderArgs
);
```

### 使用 SQL 查询加载文档

query 参数允许用户指定自定义 SQL 查询，该查询可以包含过滤器，以从数据库加载特定文档。

```js theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
const documentLoaderArgs: PostgresLoaderOptions = {
  query: "SELECT * FROM my_fruit_table",
  contentColumns: ["fruit_name", "variety"],
  metadataColumns: [
    "fruit_id",
    "quantity_in_stock",
    "price_per_unit",
    "organic",
  ],
  format: "text",
};

const documentLoaderInstance = await PostgresLoader.initialize(
  PEInstance,
  docucumetLoaderArgs
);
```

### 设置页面内容格式

加载器返回一个文档列表，每行一个文档，页面内容采用指定的字符串格式，即文本（空格分隔的连接）、JSON、YAML、CSV 等。JSON 和 YAML 格式包含标题，而文本和 CSV 不包含字段标题。

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档](/use-these-docs)通过 MCP 连接到 Claude、VSCode 等，以获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/oss/javascript/integrations/document_loaders/web_loaders/google_cloudsql_pg.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
