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 expressions 来动态填充 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. 将“操作”设置为“执行查询”。

¥Set the Operation to Execute Query. 2. 在“选项”中,选择“查询参数”。

¥In Options, select Query Parameters. 3. 使用表达式从输入数据中选择一个数组。例如,{{ $json.input_shirt_sizes }}

¥Use an expression to select an array from the input data. For example, {{ $json.input_shirt_sizes }}. 4. 在“查询”参数中,使用 IN 结构编写查询,并在其后添加空括号。例如:

¥In the Query parameter, write your query with the IN construct with an empty set of parentheses. For example:

1
SELECT color, shirt_size FROM shirts WHERE shirt_size IN ();
5. 在 IN 括号内,使用表达式动态创建基于索引的占位符(例如 $1$2$3),用于表示查询参数数组中的项目数量。由于占位符变量的索引为 1,你可以通过将每个数组索引加 1 来完成此操作:

¥Inside of the IN parentheses, use an expression to dynamically create index-based placeholders (like $1, $2, and $3) for the number of items in your query parameter array. You can do this by increasing each array index by one since the placeholder variables are 1 indexed:

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 有助于避免在不同的日期表示形式和系统之间转换日期时,因时区转换而造成的混淆。

¥Use UTC when storing and passing dates: Using UTC helps avoid confusion over timezone conversions when converting dates between different representations and systems.

  • 设置执行时区:使用 环境变量(对于自托管)或 settings(对于 n8n 云)在 n8n 中设置全局时区。你可以在 工作流设置 中设置工作流特定的时区。

¥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.

  • 使用 ISO 8601 格式:ISO 8601 格式 将日期、月份、年份、小时、分钟和秒编码成一个标准化的字符串。n8n 将日期作为字符串在节点之间传递,并使用 Luxon 进行解析。日期。如果你需要显式转换为 ISO 8601 格式,可以使用 日期和时间节点 并将自定义格式设置为字符串 yyyy-MM-dd'T'HH:mm:ss

¥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.

将日期列输出为日期字符串,而不是 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.