Skip to content

Postgres 节点常见问题(Postgres node common issues)#

这里是一些关于 Postgres 节点 的常见错误和问题,以及解决或排查它们的步骤。

🌐 Here are some common errors and issues with the Postgres node and steps to resolve or troubleshoot them.

使用参数动态填充 SQL IN 组(Dynamically populate SQL IN groups with parameters)#

在 Postgres 中,你可以使用 SQL IN 比较结构 来对值组进行比较:

🌐 In Postgres, you can use the SQL IN comparison construct to make comparisons between groups of values:

1
SELECT color, shirt_size FROM shirts WHERE shirt_size IN ('small', 'medium', 'large');

虽然你可以在查询中使用 n8n 表达式 来动态填充 IN 组中的值,但将其与 查询参数 结合使用可以提供额外的保护,因为它会自动清理输入。

🌐 While you can use n8n expressions in your query to dynamically populate the values in an IN group, combining this with query parameters provides extra protection by automatically sanitizing input.

要构建带有查询参数的 IN 组查询:

🌐 To construct an IN group query with query parameters:

  1. 操作 设置为 执行查询
  2. 选项 中,选择 查询参数
  3. 使用表达式从输入数据中选择一个数组。例如,{{ $json.input_shirt_sizes }}
  4. Query 参数中,使用带有一对空括号的 IN 构造来编写你的查询。例如:

    1
    SELECT color, shirt_size FROM shirts WHERE shirt_size IN ();
    
  5. IN 括号内部,使用表达式动态创建基于索引的占位符(例如 $1$2$3),其数量对应于查询参数数组中的项数。你可以通过将每个数组索引加一来实现这一点,因为占位符变量是从 1 开始索引的:

    1
    SELECT color, shirt_size FROM shirts WHERE shirt_size IN ({{ $json.input_shirt_sizes.map((i, pos) => "$" + (pos+1)).join(', ') }});
    

使用此技术,n8n 会根据数组中项目的数量自动为 IN 值创建正确数量的预处理语句占位符

🌐 With this technique, n8n automatically creates the correct number of prepared statement placeholders for the IN values according to the number of items in your array.

处理时间戳和时区(Working with timestamps and time zones)#

为避免 n8n 和 Postgres 对时间戳和时区数据的解析出现问题,请遵循以下一般提示:

🌐 To avoid complications with how n8n and Postgres interpret timestamp and time zone data, follow these general tips:

  • 在存储和传递日期时使用 UTC:使用 UTC 有助于在不同表示方式和系统之间转换日期时避免时区转换的混淆。
  • 设置执行时区:使用 环境变量(针对自托管)或在 设置 中(针对 n8n 云)设置 n8n 的全局时区。你也可以在 工作流设置 中设置工作流特定时区。
  • 使用 ISO 8601 格式ISO 8601 格式以标准化字符串编码月份中的日期、月份、年份、小时、分钟和秒。n8n 在节点之间传递日期时使用字符串,并使用 Luxon 解析日期。如果你需要显式转换为 ISO 8601,可以使用 日期和时间节点 并将自定义格式设置为字符串 yyyy-MM-dd'T'HH:mm:ss

将日期列输出为日期字符串,而不是 ISO 日期时间字符串(Outputting Date columns as date strings instead of ISO datetime strings)#

n8n 使用 pg 来集成 Postgres,这会影响 n8n 如何处理来自 Postgres 的日期、时间戳及相关类型。

🌐 n8n's uses the pg package to integrate with Postgres, which affects how n8n processes date, timestamp, and related types from Postgres.

pg 包默认将 DATE 值解析为 new Date(row_value),生成遵循 ISO 8601 日期时间字符串 格式的日期。例如,日期 2025-12-25 可能生成 2025-12-25T23:00:00.000Z 的日期时间字符串,具体取决于实例的时区设置。

🌐 The pg package parses DATE values into new Date(row_value) by default, which produces a date that follows the ISO 8601 datetime string format. For example, a date of 2025-12-25 might produce a datetime sting of 2025-12-25T23:00:00.000Z depending on the instance's timezone settings.

为了解决这个问题,请使用 Postgres TO_CHAR 函数 在查询时将日期格式化为预期格式:

🌐 To work around this, use the Postgres TO_CHAR function to format the date into the expected format at query time:

1
SELECT TO_CHAR(date_col, 'YYYY-MM-DD') AS date_col_as_date FROM table_with_date_col

这将生成不带时间或时区组件的日期字符串。继续前面的示例,通过这种类型转换,日期 2025-12-25 将生成字符串 2025-12-25。你可以在pg日期包文档中了解更多信息。

🌐 This will produce the date as a string without the time or timezone components. To continue the earlier example, with this casting, a date of 2025-12-25 would produce the string 2025-12-25. You can find out more in the pg package documentation on dates.