Skip to content

Instantly share code, notes, and snippets.

@sykp241095
Created March 18, 2026 08:28
Show Gist options
  • Select an option

  • Save sykp241095/a194a53a2ecae41e25d713dcac2b1fe4 to your computer and use it in GitHub Desktop.

Select an option

Save sykp241095/a194a53a2ecae41e25d713dcac2b1fe4 to your computer and use it in GitHub Desktop.
db9 Bug Report: postgres.js write operations fail with CONNECTION_CLOSED

db9 Bug Report: postgres.js write operations fail with CONNECTION_CLOSED

Environment

  • Runtime: Bun 1.3.10 (macOS arm64)
  • Client library: postgres (postgres.js) v3.4.8
  • Connection: postgresql://<user>@pg.db9.io:5433/postgres

Issue

All write operations (INSERT, UPDATE) using postgres.js fail with CONNECTION_CLOSED, while read operations (SELECT) work fine on the same connection.

Reproduction

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();

Tested Configurations

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

Workaround

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 Details

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)

Summary

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment