8
n8n 中文网amn8n.com

将Postgres备份为CSV到GitHub

高级

这是一个DevOps, Multimodal AI领域的自动化工作流,包含 17 个节点。主要使用 If, Code, Github, Postgres, ItemLists 等节点。 每日将Postgres表以CSV格式备份到GitHub

前置要求
  • GitHub Personal Access Token
  • PostgreSQL 数据库连接信息
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "oBJaQvcNxODDoAl1",
  "meta": {
    "instanceId": "52254486b159b349334953c1738da94e90477c7604aa8db2062d11afc0120739",
    "templateCredsSetupCompleted": true
  },
  "name": "将postgres备份为csv到github",
  "tags": [],
  "nodes": [
    {
      "id": "20477146-5542-4701-a92f-4300c95a0c5d",
      "name": "每日计划",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -1712,
        -96
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "hours",
              "hoursInterval": 24
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "3f5ec59d-374f-421c-a333-155c06443e5d",
      "name": "列出表格",
      "type": "n8n-nodes-base.postgres",
      "position": [
        -544,
        64
      ],
      "parameters": {
        "table": {
          "__rl": true,
          "mode": "name",
          "value": "={{ $json.table_name }}"
        },
        "schema": {
          "__rl": true,
          "mode": "list",
          "value": "public"
        },
        "options": {},
        "operation": "select",
        "returnAll": true
      },
      "credentials": {
        "postgres": {
          "id": "0bZEnNx2jwoEznOw",
          "name": "pg - n8n-discord-trigger-bot"
        }
      },
      "typeVersion": 2.6
    },
    {
      "id": "8d8af1f2-5b28-449d-a354-a624f9e2a53c",
      "name": "遍历项目",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        -912,
        -96
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "680847b4-e704-4728-8ee2-44642d931d58",
      "name": "代码",
      "type": "n8n-nodes-base.code",
      "position": [
        -752,
        64
      ],
      "parameters": {
        "jsCode": "return $input.all();"
      },
      "typeVersion": 2
    },
    {
      "id": "0ec30d45-c281-4d65-a966-29f615eade3a",
      "name": "转换为文件1",
      "type": "n8n-nodes-base.convertToFile",
      "position": [
        -336,
        64
      ],
      "parameters": {
        "options": {
          "fileName": "={{ $('List tables1').item.json.table_name }}"
        },
        "binaryPropertyName": "=data"
      },
      "typeVersion": 1.1
    },
    {
      "id": "09466877-0cdb-4011-809f-87c696ab3717",
      "name": "列出表格1",
      "type": "n8n-nodes-base.postgres",
      "position": [
        -1104,
        -96
      ],
      "parameters": {
        "table": {
          "__rl": true,
          "mode": "list",
          "value": "tables",
          "cachedResultName": "tables"
        },
        "where": {
          "values": [
            {
              "value": "public",
              "column": "table_schema"
            }
          ]
        },
        "schema": {
          "__rl": true,
          "mode": "list",
          "value": "information_schema",
          "cachedResultName": "information_schema"
        },
        "options": {},
        "operation": "select"
      },
      "credentials": {
        "postgres": {
          "id": "0bZEnNx2jwoEznOw",
          "name": "pg - n8n-discord-trigger-bot"
        }
      },
      "typeVersion": 2.6
    },
    {
      "id": "014fa66c-4acc-4698-97cc-142efaae9376",
      "name": "从仓库列出文件[GITHUB]",
      "type": "n8n-nodes-base.github",
      "position": [
        -1536,
        -96
      ],
      "webhookId": "f7310d6a-1573-4848-9757-f9a75e359e73",
      "parameters": {
        "owner": {
          "__rl": true,
          "mode": "name",
          "value": "user"
        },
        "filePath": "=",
        "resource": "file",
        "operation": "list",
        "repository": {
          "__rl": true,
          "mode": "list",
          "value": "github-repo",
          "cachedResultUrl": "https://github.com/user/github-repo",
          "cachedResultName": "github-repo"
        },
        "authentication": "oAuth2"
      },
      "credentials": {
        "githubOAuth2Api": {
          "id": "g3sESgnuArjRvV8F",
          "name": "GitHub account"
        }
      },
      "typeVersion": 1,
      "alwaysOutputData": true
    },
    {
      "id": "5942926d-b940-4ba4-a0c1-7209c04c0a3c",
      "name": "合并文件名[GITHUB]",
      "type": "n8n-nodes-base.itemLists",
      "position": [
        -1344,
        -96
      ],
      "parameters": {
        "options": {},
        "operation": "aggregateItems",
        "fieldsToAggregate": {
          "fieldToAggregate": [
            {
              "fieldToAggregate": "name"
            }
          ]
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "00aa6a0e-b5c1-4093-8ee0-94b35bcdd934",
      "name": "便签 2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1584,
        -256
      ],
      "parameters": {
        "color": 7,
        "width": 390,
        "height": 547,
        "content": "## 获取当前表格列表"
      },
      "typeVersion": 1
    },
    {
      "id": "863c59ef-6ce2-4c22-8c0e-00b9abcfdbd9",
      "name": "拆分为单个项目",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        -80,
        -112
      ],
      "parameters": {
        "options": {},
        "batchSize": 1
      },
      "typeVersion": 2
    },
    {
      "id": "ccee7881-77c8-419e-ad99-ac3bb113ae6b",
      "name": "检查文件是否存在于仓库中",
      "type": "n8n-nodes-base.if",
      "position": [
        160,
        -128
      ],
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $node['Combine file names [GITHUB]'].json.name }}",
              "value2": "={{ $binary.data.fileName }}",
              "operation": "contains"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "f6d53dcc-1e40-43f5-b793-4d06a55ccbed",
      "name": "更新文件[GITHUB]",
      "type": "n8n-nodes-base.github",
      "position": [
        384,
        -128
      ],
      "webhookId": "fb5f5095-cf60-4421-8b83-91041cfc8929",
      "parameters": {
        "owner": {
          "__rl": true,
          "mode": "name",
          "value": "user"
        },
        "filePath": "={{ $binary.data.fileName }}",
        "resource": "file",
        "operation": "edit",
        "binaryData": true,
        "repository": {
          "__rl": true,
          "mode": "list",
          "value": "github-repo",
          "cachedResultUrl": "https://github.com/user/github-repo",
          "cachedResultName": "github-repo"
        },
        "commitMessage": "=backup-{{ $now.toMillis() }}",
        "authentication": "oAuth2"
      },
      "credentials": {
        "githubOAuth2Api": {
          "id": "g3sESgnuArjRvV8F",
          "name": "GitHub account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "ebcd65d3-37ac-4d47-90dc-fede45adb9c0",
      "name": "上传文件[GITHUB]",
      "type": "n8n-nodes-base.github",
      "position": [
        384,
        80
      ],
      "webhookId": "17b56fbd-32da-4e39-ae22-8c01a7b4bbb6",
      "parameters": {
        "owner": {
          "__rl": true,
          "mode": "name",
          "value": "user"
        },
        "filePath": "={{ $binary.data.fileName }}",
        "resource": "file",
        "binaryData": true,
        "repository": {
          "__rl": true,
          "mode": "list",
          "value": "github-repo",
          "cachedResultUrl": "https://github.com/user/github-repo",
          "cachedResultName": "github-repo"
        },
        "commitMessage": "=backup-{{ $node['Set commit date'].json.commitDate }}",
        "authentication": "oAuth2"
      },
      "credentials": {
        "githubOAuth2Api": {
          "id": "g3sESgnuArjRvV8F",
          "name": "GitHub account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "6bcbeffb-b036-45f3-88a8-dd5f1ac0fa39",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1168,
        -256
      ],
      "parameters": {
        "color": 5,
        "width": 998,
        "height": 547,
        "content": "## 获取postgres表格数据"
      },
      "typeVersion": 1
    },
    {
      "id": "88cc96e5-fc5b-4d28-8e57-82c193efb19d",
      "name": "便签 1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -144,
        -256
      ],
      "parameters": {
        "color": 7,
        "width": 694,
        "height": 547,
        "content": "## 创建现有文件列表"
      },
      "typeVersion": 1
    },
    {
      "id": "4b4f32d2-2138-4d9a-8d48-6b35b387270a",
      "name": "便签 3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -848,
        320
      ],
      "parameters": {
        "color": 7,
        "width": 374,
        "height": 195,
        "content": "![postgres表格](https://articles.emp0.com/wp-content/uploads/2025/08/backup-postgres-to-github-tables.png)"
      },
      "typeVersion": 1
    },
    {
      "id": "02ddc44f-7c23-4022-9978-c43876593274",
      "name": "便签 4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -144,
        320
      ],
      "parameters": {
        "color": 7,
        "width": 694,
        "height": 259,
        "content": "![github备份](https://articles.emp0.com/wp-content/uploads/2025/08/backup-postgres-to-github-repo.png)"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "747a3b1e-5b2a-4f61-875c-250bd44fbf01",
  "connections": {
    "Code": {
      "main": [
        [
          {
            "node": "List tables",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "List tables": {
      "main": [
        [
          {
            "node": "Convert to File1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "List tables1": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Daily Schedule": {
      "main": [
        [
          {
            "node": "List files from repository [GITHUB]",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [
          {
            "node": "Split to single items",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert to File1": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update file [GITHUB]": {
      "main": [
        [
          {
            "node": "Split to single items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Upload file [GITHUB]": {
      "main": [
        [
          {
            "node": "Split to single items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split to single items": {
      "main": [
        [
          {
            "node": "Check if file exists in repository",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Combine file names [GITHUB]": {
      "main": [
        [
          {
            "node": "List tables1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check if file exists in repository": {
      "main": [
        [
          {
            "node": "Update file [GITHUB]",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Upload file [GITHUB]",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "List files from repository [GITHUB]": {
      "main": [
        [
          {
            "node": "Combine file names [GITHUB]",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

高级 - 开发运维, 多模态 AI

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
高级
节点数量17
分类2
节点类型9
难度说明

适合高级用户,包含 16+ 个节点的复杂工作流

作者
Jay Emp0

Jay Emp0

@jay-emp0

AI and Automation developer. Im implementing n8n and ai tools to automate marketing and sales in companies

外部链接
在 n8n.io 查看

分享此工作流