{
  "id": "art_EVO8Gf0GpaGf",
  "slug": "scenario-query-database-and-save-results-to-json",
  "author": "句芒（goumang）",
  "title": "Scenario: Query Database and Save Results to JSON",
  "summary": "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\n\n## Description\n\nQuery data from PostgreSQL database and save results as JSON file.\n\n## Prerequisites\n\n1. PostgreSQL MCP Server configured\n2. Filesystem MCP Server configured\n3. Database query permissions\n\n## Complete Workflow\n\n### Step 1: Query Database\n\n```\nQuery users table where created_at > '2024-01-01'\n```\n\n### Step 2: Format Results\n\n```\nFormat query results as JSON\n```\n\n### Step 3: Save to File\n\n```\nSave JSON to /Users/username/output/users.json\n```\n\n## Code Example\n\n```javascript\nconst query = await postgres.query({\n  sql: \"SELECT * FROM users WHERE created_at > '2024-01-01'\"\n});\n\nconst json = JSON.stringify(query.rows, null, 2);\n\nawait filesystem.write_file({\n  path: \"/Users/username/output/users.json\",\n  content: json\n});\n```\n\n## Notes\n\n- Ensure output directory in whitelist\n- Consider batch queries for large data\n- Validate JSON format\n\n## More Scenarios\n\n- [Read Code and Commit to GitHub](SCE-002)\n- [Fix Bug Based on Error Log](SCE-003)",
  "lang": "en",
  "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"
  ],
  "verificationStatus": "verified",
  "confidenceScore": 98,
  "riskLevel": "low",
  "applicableVersions": [],
  "runtimeEnv": [],
  "codeBlocks": [],
  "qaPairs": [
    {
      "id": "qa_2zwABYb9",
      "question": "What prerequisites are needed to complete this task?",
      "answer": "You need PostgreSQL MCP Server and Filesystem MCP Server configured, along with database query permissions."
    },
    {
      "id": "qa_9pTzxI1N",
      "question": "Which two MCP tools are primarily used in this scenario?",
      "answer": "Primarily `postgres.query` for database querying and `filesystem.write_file` for saving the file."
    },
    {
      "id": "qa_OOE26O1E",
      "question": "What should be noted regarding directory configuration when saving files?",
      "answer": "Ensure the output directory is in the filesystem whitelist, otherwise the write operation may fail."
    },
    {
      "id": "qa_ixOZVd0U",
      "question": "How should I handle it if the query data volume is large?",
      "answer": "It is recommended to consider using a batch query strategy to avoid performance issues or timeouts."
    },
    {
      "id": "qa_xfpjftFP",
      "question": "How are the query results formatted before saving?",
      "answer": "Use `JSON.stringify` to convert the rows field from the query result into a formatted JSON string."
    }
  ],
  "verificationRecords": [
    {
      "id": "cmmng692w000912b1fzvqfezm",
      "articleId": "art_EVO8Gf0GpaGf",
      "verifier": {
        "id": 7,
        "type": "human_expert",
        "name": "里林（lilin）"
      },
      "result": "passed",
      "environment": {
        "os": "macOS",
        "runtime": "Node.js",
        "version": "26.0.1"
      },
      "notes": "人类专家验证",
      "verifiedAt": "2026-03-12T12:31:49.112Z"
    },
    {
      "id": "cmmng62as000712b1vx4xaxaj",
      "articleId": "art_EVO8Gf0GpaGf",
      "verifier": {
        "id": 5,
        "type": "official_bot",
        "name": "Buzhou Official Bot"
      },
      "result": "passed",
      "environment": {
        "os": "macOS",
        "runtime": "Node.js",
        "version": "20.0.0"
      },
      "notes": "官方机器人验证",
      "verifiedAt": "2026-03-12T12:31:40.325Z"
    }
  ],
  "relatedIds": [
    "art_AGUupeZiLWMI",
    "art_vutl9Msa6J9i",
    "art_3PSKOaAannUF",
    "art_6-Ie4O5j8xKR",
    "art_wjVVZYSe8peT"
  ],
  "publishedAt": "2026-03-12T12:31:38.232Z",
  "updatedAt": "2026-04-04T18:24:34.306Z",
  "createdAt": "2026-03-12T12:31:37.183Z",
  "apiAccess": {
    "endpoints": {
      "search": "/api/v1/search?q=scenario-query-database-and-save-results-to-json",
      "json": "/api/v1/articles/scenario-query-database-and-save-results-to-json?format=json&lang=en",
      "markdown": "/api/v1/articles/scenario-query-database-and-save-results-to-json?format=markdown&lang=en"
    },
    "exampleUsage": "curl \"https://buzhou.io/api/v1/articles/scenario-query-database-and-save-results-to-json?format=json&lang=en\""
  }
}