Postgres 节点常见问题#
以下是 Postgres 节点 的一些常见错误和问题以及解决或排除故障的步骤。
使用参数动态填充 SQL IN 组#
在 Postgres 中,您可以使用 SQL IN 比较构造在值组之间进行比较:
1 | |
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 组查询:
- 将Operation 操作设置为Execute Query 执行查询。
- 在Options 选项中,选择Query Parameters 查询参数。
- 使用表达式从输入数据中选择数组。例如,
{{ $json.input_shirt_sizes }}。 - 在Query 查询参数中,用空括号编写带有
IN构造的查询。例如:1SELECT color, shirt_size FROM shirts WHERE shirt_size IN (); - 在
IN括号内,使用表达式为查询参数数组中的项目数动态创建基于索引的占位符(如$1、$2和$3)。您可以通过将每个数组索引增加一来做到这一点,因为占位符变量是从 1 开始索引的:1SELECT color, shirt_size FROM shirts WHERE shirt_size IN ({{ $json.input_shirt_sizes.map((i, pos) => "$" + (pos+1)).join(', ') }});
使用这种技术,n8n 会根据数组中的项目数自动为 IN 值创建正确数量的预处理语句占位符。
处理时间戳和时区#
为了避免 n8n 和 Postgres 解释时间戳和时区数据时的复杂性,请遵循以下一般提示:
- Use UTC when storing and passing dates: Using UTC helps avoid confusion over timezone conversions when converting dates between different representations and systems.
- Set the execution timezone: Set the global timezone in n8n using either environment variables (for self-hosted) or in the settings (for n8n Cloud). You can set a workflow-specific timezone in the workflow settings.
- Use ISO 8601 format: The ISO 8601 format encodes the day of the month, month, year, hour, minutes, and seconds in a standardized string. n8n passes dates between nodes as strings and uses Luxon to parse dates. If you need to cast to ISO 8601 explicitly, you can use the Date & Time node and a custom format set to the string
yyyy-MM-dd'T'HH:mm:ss.
将 Date 列输出为日期字符串而不是 ISO 日期时间字符串#
n8n 使用 pg 包 集成 Postgres,这会影响 n8n 如何处理来自 Postgres 的 date、timestamp 及相关类型。
pg 包默认会将 DATE 类型的值解析为 new Date(row_value),这会生成一个遵循 ISO 8601 日期时间字符串 格式的日期。例如,2025-12-25 这个日期,可能会根据实例的时区设置,生成 2025-12-25T23:00:00.000Z 这样的日期时间字符串。
要规避这个问题,可以在查询时使用 Postgres 的 TO_CHAR 函数 将日期格式化为期望的格式:
1 | |
这样会将日期以不带时间和时区部分的字符串输出。继续前面的例子,2025-12-25 经过这种转换后会输出为字符串 2025-12-25。你可以在 pg 包关于日期的文档 中了解更多信息。