- Runtime: Bun 1.3.10 (macOS arm64)
- Client library: postgres (postgres.js) v3.4.8
- Connection:
postgresql://<user>@pg.db9.io:5433/postgres
All write operations (INSERT, UPDATE) using postgres.js fail with CONNECTION_CLOSED, while read operations (SELECT) work fine on the same connection.
import postgres from "postgres";
const sql = postgres("postgresql://<user>:<pass>@pg.db9.io:5433/postgres", {
max: 1,
idle_timeout: 30,
connect_timeout: 10,
});
// ✅ This works
const r1 = await sql`SELECT 1 as ok`;
console.log("SELECT OK:", r1[0]); // { ok: 1 }
// ❌ This fails
try {
const r2 = await sql`INSERT INTO test_table (name) VALUES ('hello') RETURNING *`;
console.log("INSERT OK:", r2[0]);
} catch (e) {
console.log("INSERT FAIL:", e.message);
// Output: "write CONNECTION_CLOSED pg.db9.io:5433"
}
await sql.end();| Config | SELECT | INSERT |
|---|---|---|
postgres(url) (no options) |
✅ | ❌ CONNECTION_CLOSED |
postgres(url, { ssl: true }) |
✅ | ❌ CONNECTION_CLOSED |
postgres(url, { ssl: 'require' }) |
✅ | ❌ CONNECTION_CLOSED |
postgres(url + '?sslmode=require') |
✅ | ❌ CONNECTION_CLOSED |
Switching to pg (node-postgres) with ssl: { rejectUnauthorized: false } works for both reads and writes:
import pg from "pg";
const pool = new pg.Pool({
connectionString: "postgresql://<user>:<pass>@pg.db9.io:5433/postgres",
ssl: { rejectUnauthorized: false },
});
// ✅ Both work
await pool.query("SELECT 1");
await pool.query("INSERT INTO test_table (name) VALUES ('hello') RETURNING *");error: write CONNECTION_CLOSED pg.db9.io:5433
errno: "CONNECTION_CLOSED",
address: [ "pg.db9.io" ],
port: [ 5433 ],
code: "CONNECTION_CLOSED"
at closed (postgres/src/connection.js:453:57)
postgres.js (the postgres npm package) cannot perform write operations against db9, while the pg npm package works fine with ssl: { rejectUnauthorized: false }. The issue appears to be related to how postgres.js handles TLS/SSL connections to db9, specifically during write operations where the socket is closed mid-write.