Repository navigation
Description
What version of Bun is running?
1.3.11
What platform is your computer?
Darwin 25.2.0 arm64 arm
What steps can reproduce the bug?
Description
When a JavaScript Date object is passed as a query parameter to Bun.SQL with prepare: false — either in a template-literal query or inside a row object passed to sql(rows) — Bun serializes it using Date.prototype.toString() (locale / timezone-dependent string) instead of Date.prototype.toISOString() (RFC 3339 / ISO 8601).
This does not reproduce with prepare: true (the default): when prepared statements are enabled, Bun receives the server's ParameterDescription before encoding the Bind message, so it has type information and uses the correct binary path. With prepare: false, Parse and Bind are pipelined with no type info available, and Date falls through to a toString() fallback.
Most databases, including PostgreSQL and GreptimeDB, cannot parse the locale format and return a parse error.
Reproduction
import { SQL } from 'bun';
const sql = new SQL('postgres://user@localhost:5432/db', { prepare: false });
const t = new Date('2024-01-15T12:30:45.000Z');
// In a WHERE clause parameter:
const rows = await sql`SELECT * FROM my_table WHERE created_at = ${t}`;
// Sends: "Mon Jan 15 2024 12:30:45 GMT+0000 (Coordinated Universal Time)"
// PostgreSQL error: invalid input syntax for type timestamp
// In a row object passed to sql(rows):
await sql`INSERT INTO my_table ${sql([{ created_at: t }])}`;
// Same incorrect serializationThe workaround is to call .toISOString() manually at every parameter site, or to convert dates before building row objects:
// Workaround
await sql`SELECT * FROM my_table WHERE created_at = ${t.toISOString()}`;Expected behavior
Date objects should be serialized as an ISO 8601 / RFC 3339 string when sent as query parameters — a format PostgreSQL-compatible databases can parse. Both other major JS drivers do this:
postgres.jshardcodes.toISOString()insrc/types.js(types.date.serialize), always producing a UTC string ending inZ(e.g."2024-01-15T12:30:45.000Z").pg(node-postgres) builds an ISO 8601 string with the local timezone offset (e.g."2024-01-15T12:30:45.000-05:00") — not identical to postgres.js, but still ISO 8601 and accepted by PostgreSQL.
Actual behavior
Bun serializes Date as the locale string produced by .toString(), e.g.:
Mon Jan 15 2024 12:30:45 GMT+0000 (Coordinated Universal Time)
This format is rejected by PostgreSQL-compatible databases with errors such as:
Unable to parse datetime: Mon Jan 15 2024 12:30:45 GMT+0000
invalid input syntax for type timestamp
Environment
- Bun: 1.3.11 (
bun --version) - Platform: macOS arm64
- Database: GreptimeDB v1.0.0-rc.2 (PostgreSQL wire protocol), also reproducible against standard PostgreSQL
Notes
- The issue affects both template-literal parameters (
sql\... ${date}`) and row objects passed tosql(rows)` for bulk inserts. sql.unsafe()with a plain string query is not affected (no parameter serialization).- The locale string format varies by system timezone, making this a latent bug that may only surface in environments where the timezone offset is non-zero.
Activity
[-]Bug: Bun.SQL serializes `Date` parameters via `.toString()` instead of `.toISOString()`[/-][+]Bug: Bun.SQL serializes `Date` parameters via `.toString()` instead of `.toISOString()` for `prepare:false`[/+]robobun commentedon Apr 8, 2026
✅ Rebased onto main —
1a22fddca5(single squashed commit).CI build 44638 failure is unrelated — the only non-flaky annotation is a Windows ARM64 crash in
test/js/bun/util/v8-heap-snapshot.test.tswith "Illegal instruction at address 0x7FF7F64BE060" and 9.13GB peak RSS. That is a Bun runtime panic in the heap-snapshot code path on ARM64 Windows, not touched by this PR.This PR only modifies:
src/bun.js/bindings/JSValue.zig— widen ISO buffer to[64]u8, fixgetDateNowISOStringABI mismatchsrc/bun.js/bindings/bindings.cpp—JSC__JSValue__toISOStringwrites to callerbufsrc/sql/postgres/PostgresRequest.zig— Date → ISO 8601 inwriteBindelse branchtest/regression/issue/29010.test.ts— regression testNone of these interact with v8 heap snapshots, memory management, or Windows ARM64 codegen. The rest of the annotations (bake stress, bundler_defer, terminal) are the same pre-existing Windows flakes from prior runs, all tagged
style: WARNING, context: flakyin the buildkite annotation data.Pre-merge checks are all green (Out of Scope warning cleared after the rebase) and coderabbit LGTM with no actionable comments.
sql: serialize Date parameters as ISO 8601 in Postgres text format
sql: serialize Date parameters as ISO 8601 in Postgres text format
heiwen commentedon Apr 12, 2026
Linking to the Bun.SQL (Postgres) tracking issue: #15088
sql: serialize Date parameters as ISO 8601 in Postgres text format
juanluispaz commentedon May 29, 2026
Heads up — the same Date#toString() serialization also reaches PostgreSQL through sql.unsafe(query, params), which the issue description currently excludes ("sql.unsafe() with plain string queries is not affected"). In practice it is, regardless of the prepare setting.
Repro
bun --version: 1.3.14 · server: postgres:18-alpine
Output
So both prepare:true (the default) and prepare:false reproduce the same serialization; only pre-stringifying via Date#toISOString() works around it. Same root cause and same error wording as the original report, just an additional code path that the current issue scope rules out.
Would it make sense to widen the scope of this issue (or have #29013 also cover the unsafe(query, params) path) so the fix lands on the shared serializer?
navidemad commentedon Sep 2, 2026
Still reproducible on Bun 1.4.0 (macOS arm64 and linux x64 in Docker
oven/bun:1-alpine), PostgreSQL 16. Two additional data points from a production collector that had to switch toprepare: falseto work around #28980:1.
Date→toString()(as reported):2. Plain objects bound to a
jsonbcolumn are serialized as"[object Object]", so the same workload also fails on JSON columns:Both happen with the bulk-insert helper (
sql`INSERT INTO events ${sql(rows)}`) and with single-parameter queries. Withprepare: truethe same rows insert fine, so the text-mode parameter encoding path is the only one affected.Minimal repro:
Workaround we shipped: pre-serialize on our side (
date.toISOString()andJSON.stringify(obj)), PostgreSQL casts the text totimestamptz/jsonbitself. It would be good for theprepare: falsepath to use the same encoders as the prepared path (ISO 8601 forDate, JSON for objects/arrays).sql(postgres): send a Date bound in text format as ISO 8601
SQL:prepare: falsechanges parameter encoding —Dateand plain objects stop binding #39450 as a duplicate of this issuetetsuodev commentedon Oct 12, 2026
timecolumns fail too, with a different message, and both cases reach query builders that bind throughsql.unsafe()with the defaultprepare:Bun 1.4.3, PostgreSQL 18.6, run with
TZ=UTC.sql: consolidate the open Bun.sql, bun:sqlite and node:sqlite pull re…
Drop the Bun SQL workarounds fixed in Bun 1.4.4