mirror of
https://github.com/oven-sh/bun
synced 2026-02-02 15:08:46 +00:00
61 lines
2.0 KiB
Plaintext
61 lines
2.0 KiB
Plaintext
---
|
|
title: Use Neon's Serverless Postgres with Bun
|
|
sidebarTitle: Neon Serverless Postgres with Bun
|
|
mode: center
|
|
---
|
|
|
|
[Neon](https://neon.tech/) is a fully managed serverless Postgres. Neon separates compute and storage to offer modern developer features such as autoscaling, branching, bottomless storage, and more.
|
|
|
|
---
|
|
|
|
Get started by creating a project directory, initializing the directory using `bun init`, and adding the [Neon serverless driver](https://github.com/neondatabase/serverless/) as a project dependency.
|
|
|
|
```sh terminal icon="terminal"
|
|
mkdir bun-neon-postgres
|
|
cd bun-neon-postgres
|
|
bun init -y
|
|
bun add @neondatabase/serverless
|
|
```
|
|
|
|
---
|
|
|
|
Create a `.env.local` file and add your [Neon Postgres connection string](https://neon.tech/docs/connect/connect-from-any-app) to it.
|
|
|
|
```ini .env.local icon="settings"
|
|
DATABASE_URL=postgresql://usertitle:password@ep-adj-noun-guid.us-east-1.aws.neon.tech/neondb?sslmode=require
|
|
```
|
|
|
|
---
|
|
|
|
Paste the following code into your project's `index.ts` file.
|
|
|
|
```ts index.ts icon="/icons/typescript.svg"
|
|
import { neon } from "@neondatabase/serverless";
|
|
|
|
// Bun automatically loads the DATABASE_URL from .env.local
|
|
// Refer to: https://bun.com/docs/runtime/environment-variables for more information
|
|
const sql = neon(process.env.DATABASE_URL);
|
|
|
|
const rows = await sql`SELECT version()`;
|
|
|
|
console.log(rows[0].version);
|
|
```
|
|
|
|
---
|
|
|
|
Start the program using `bun ./index.ts`. The Postgres version should be printed to the console.
|
|
|
|
```sh terminal icon="terminal"
|
|
bun ./index.ts
|
|
```
|
|
|
|
```txt
|
|
PostgreSQL 16.2 on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
|
|
```
|
|
|
|
---
|
|
|
|
This example used the Neon serverless driver's SQL-over-HTTP functionality. Neon's serverless driver also exposes `Client` and `Pool` constructors to enable sessions, interactive transactions, and node-postgres compatibility.
|
|
|
|
Refer to [Neon's documentation](https://neon.tech/docs/serverless/serverless-driver) for a complete overview of the serverless driver.
|