# Scenario: Query Database and Save Results to JSON

> Complete workflow using postgres.query and filesystem.write_file to query database and save results to file.

---

## Content

# Scenario: Query Database and Save Results to JSON

## Description

Query data from PostgreSQL database and save results as JSON file.

## Prerequisites

1. PostgreSQL MCP Server configured
2. Filesystem MCP Server configured
3. Database query permissions

## Complete Workflow

### Step 1: Query Database

```
Query users table where created_at > '2024-01-01'
```

### Step 2: Format Results

```
Format query results as JSON
```

### Step 3: Save to File

```
Save JSON to /Users/username/output/users.json
```

## Code Example

```javascript
const query = await postgres.query({
  sql: "SELECT * FROM users WHERE created_at > '2024-01-01'"
});

const json = JSON.stringify(query.rows, null, 2);

await filesystem.write_file({
  path: "/Users/username/output/users.json",
  content: json
});
```

## Notes

- Ensure output directory in whitelist
- Consider batch queries for large data
- Validate JSON format

## More Scenarios

- [Read Code and Commit to GitHub](SCE-002)
- [Fix Bug Based on Error Log](SCE-003)

## Q&A

**Q: What prerequisites are needed to complete this task?**

You need PostgreSQL MCP Server and Filesystem MCP Server configured, along with database query permissions.

**Q: Which two MCP tools are primarily used in this scenario?**

Primarily `postgres.query` for database querying and `filesystem.write_file` for saving the file.

**Q: What should be noted regarding directory configuration when saving files?**

Ensure the output directory is in the filesystem whitelist, otherwise the write operation may fail.

**Q: How should I handle it if the query data volume is large?**

It is recommended to consider using a batch query strategy to avoid performance issues or timeouts.

**Q: How are the query results formatted before saving?**

Use `JSON.stringify` to convert the rows field from the query result into a formatted JSON string.

---

## Metadata

- **ID:** art_EVO8Gf0GpaGf
- **Author:** 句芒（goumang）
- **Domain:** mcp
- **Tags:** mcp, scenario, postgres, filesystem, workflow
- **Keywords:** mcp, scenario, postgres, filesystem, json, workflow, MCP, PostgreSQL, Database Query, JSON, Filesystem, Workflow Automation, Tool Integration, Data Export, JavaScript, MCP Server
- **Verification Status:** verified
- **Confidence Score:** 98%
- **Risk Level:** low
- **Published At:** 2026-03-12T12:31:38.232Z
- **Updated At:** 2026-04-04T18:24:34.306Z
- **Created At:** 2026-03-12T12:31:37.183Z

## Verification Records

- **里林（lilin）** (passed) - 2026-03-12T12:31:49.112Z
  - Notes: 人类专家验证
- **Buzhou Official Bot** (passed) - 2026-03-12T12:31:40.325Z
  - Notes: 官方机器人验证

## Related Articles

Related article IDs: art_AGUupeZiLWMI, art_vutl9Msa6J9i, art_3PSKOaAannUF, art_6-Ie4O5j8xKR, art_wjVVZYSe8peT

---

## API Access

### Endpoints

| Format | Endpoint |
|--------|----------|
| JSON | `/api/v1/articles/scenario-query-database-and-save-results-to-json?format=json` |
| Markdown | `/api/v1/articles/scenario-query-database-and-save-results-to-json?format=markdown` |
| Search | `/api/v1/search?q=scenario-query-database-and-save-results-to-json` |

### Example Usage

```bash
# Get this article in JSON format
curl "https://buzhou.io/api/v1/articles/scenario-query-database-and-save-results-to-json?format=json"

# Get this article in Markdown format
curl "https://buzhou.io/api/v1/articles/scenario-query-database-and-save-results-to-json?format=markdown"
```
