Bun "PostgresError" from bun:sql / postgres in CI
A Bun test that talks to Postgres (via bun:sql or the postgres client) failed with a PostgresError. In CI this is almost always a missing or misconfigured database service, not a query bug.
What this error means
bun test fails with "PostgresError" (connection refused, database does not exist, or auth failure) when a test opens a database connection in CI.
Terminal
(fail) users > inserts a row
PostgresError: connection refused
at connect (bun:sql)Common causes
No Postgres service is running in the job
The workflow has no Postgres service container, so the connection is refused when the test dials the database.
A wrong or missing connection string
DATABASE_URL points at the wrong host/port/credentials, or is unset, so the client cannot authenticate or reach the server.
How to fix it
Add a Postgres service and connection URL
- Declare a Postgres
servicescontainer in the job. - Set
DATABASE_URLto that service host and port. - Wait for readiness before running tests.
.github/workflows/ci.yml
services:
postgres:
image: postgres:16
env: { POSTGRES_PASSWORD: postgres }
ports: ['5432:5432']
options: >-
--health-cmd pg_isready --health-interval 10sPoint the client at the service
Set the connection string in the test step env so Bun connects to the CI database.
.github/workflows/ci.yml
- run: bun test
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgresHow to prevent it
- Provide a Postgres service container for database tests.
- Set DATABASE_URL from the service in the test env.
- Gate tests on a health check so the DB is ready.
Related guides
Bun "test failed" nonzero exit code (bun test) in CIFix `bun test` failing CI - one or more tests failed, so bun test exits nonzero and fails the job. Read the f…
Bun test "ENOENT" reading a fixture / file in CIFix "ENOENT: no such file or directory" during bun test in CI - a test reads a fixture by a path that does no…
Bun "TypeError: X is not a function" (Node API gap) in CIFix Bun "TypeError: X is not a function" in CI - a Node.js API a dependency calls is not implemented (or diff…