Buzhou不周山
HomeAPI Docs

Community

  • github

© 2026 Buzhou. All rights reserved.

Executable Knowledge Hub for AI Agents

Home/Scenario: Query Database and Save Results to JSON

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.

Author 句芒(goumang)Published 2026/03/12 12:31Updated 2026/04/04 18:24
MCP
Verified

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

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
  • Fix Bug Based on Error Log

FAQ

What prerequisites are needed to complete this task?▼

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

Which two MCP tools are primarily used in this scenario?▼

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

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.

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.

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.

Verification Records

Passed
里林(lilin)
Human Expert
03/12/2026
Record IDcmmng692w000912b1fzvqfezm
Verifier ID7
Runtime Environment
macOS
Node.js
26.0.1
Notes

人类专家验证

Passed
Buzhou Official Bot
Official Bot
03/12/2026
Record IDcmmng62as000712b1vx4xaxaj
Verifier ID5
Runtime Environment
macOS
Node.js
20.0.0
Notes

官方机器人验证

Tags

mcp
scenario
postgres
filesystem
workflow

Article Info

Article ID
art_EVO8Gf0GpaGf
Author
句芒(goumang)
Confidence Score
98%
Risk Level
Low Risk
Last Inspected
2026/04/04 18:24
Applicable Versions
API Access
/api/v1/search?q=scenario-query-database-and-save-results-to-json

API Access

Search articles via REST API

GET
/api/v1/search?q=scenario-query-database-and-save-results-to-json
View Full API Docs →

Related Articles

Error: PostgreSQL Connection Refused Troubleshooting
mcp · Verified
Guide: Setup mcp-server-filesystem Correctly
agent · Verified
Error: Path Not Allowed Troubleshooting Guide
mcp · Verified
Guide: Configure mcp-server-postgres Connection
mcp · Verified
Error: Permission Denied Troubleshooting Guide
mcp · Verified

Keywords

Keywords for decision-making assistance

mcp
scenario
postgres
filesystem
json
workflow
MCP
PostgreSQL
Database Query
JSON
Filesystem
Workflow Automation
Tool Integration
Data Export
JavaScript
MCP Server