mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 10:28:47 +00:00
Add --sql-preconnect CLI flag for PostgreSQL startup connections (#21035)
Co-authored-by: Claude Bot <claude-bot@bun.sh> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: jarred-sumner-bot <220441119+jarred-sumner-bot@users.noreply.github.com> Co-authored-by: Jarred Sumner <jarred@jarredsumner.com> Co-authored-by: Jarred-Sumner <709451+Jarred-Sumner@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
70ebe75e6c
commit
e9ccc81e03
@@ -274,6 +274,23 @@ If no connection URL is provided, the system checks for the following individual
|
||||
| `PGPASSWORD` | - | (empty) | Database password |
|
||||
| `PGDATABASE` | - | username | Database name |
|
||||
|
||||
## Runtime Preconnection
|
||||
|
||||
Bun can preconnect to PostgreSQL at startup to improve performance by establishing database connections before your application code runs. This is useful for reducing connection latency on the first database query.
|
||||
|
||||
```bash
|
||||
# Enable PostgreSQL preconnection
|
||||
bun --sql-preconnect index.js
|
||||
|
||||
# Works with DATABASE_URL environment variable
|
||||
DATABASE_URL=postgres://user:pass@localhost:5432/db bun --sql-preconnect index.js
|
||||
|
||||
# Can be combined with other runtime flags
|
||||
bun --sql-preconnect --hot index.js
|
||||
```
|
||||
|
||||
The `--sql-preconnect` flag will automatically establish a PostgreSQL connection using your configured environment variables at startup. If the connection fails, it won't crash your application - the error will be handled gracefully.
|
||||
|
||||
## Connection Options
|
||||
|
||||
You can configure your database connection manually by passing options to the SQL constructor:
|
||||
|
||||
Reference in New Issue
Block a user