This guide describes how to create a project on Neon and connect to it from a Bun application. Examples are provided for using [Bun's built-in SQL client](https://bun.sh/docs/api/sql) and the [@neondatabase/serverless](/guides/postgres-serverless-serverless-driver) driver. Use the client you prefer.

:::callout{intent="note"}
The same configuration steps can be used for [Hono](https://hono.dev/docs/getting-started/bun), [Elysia](https://elysiajs.com/), and other Bun-based web frameworks.
:::

:::::steps
:::step{title="Create a project on Neon"}
If you do not have one already, create a project.

1. Navigate to the [Projects](https://console.neon.tech/app/projects) page in the console.
2. Click **New Project**.
3. Specify your project settings and click **Create Project**.
:::

:::step{title="Create a Bun project and add dependencies"}
Create a Bun project and change to the newly created directory:

```bash
mkdir bun-neon-example
cd bun-neon-example
bun init -y
```

Next, add project dependencies if you intend to use the Neon serverless driver. Otherwise, Bun's built-in `sql` client is readily available.

```bash
# No dependencies needed for Bun's built-in SQL client
```
:::

::::step{title="Store your Neon credentials"}
Add a `.env.local` file to your project directory and add your Neon connection details to it. Bun automatically loads variables from `.env`, `.env.local`, and other `.env.*` files. You can find the connection details for your database by clicking the **Connect** button in the Console nav to open the **Connect to your branch** modal. Select Bun from the **Connection string** dropdown. For more information, see [Connect from any application](/guides/postgres-connect-connect-from-any-app).

```bash
POSTGRES_URL='postgresql://[user]:[password]@[neon_hostname]/[dbname]?sslmode=require&channel_binding=require'
```

:::callout{intent="note"}
`Bun.sql` uses `POSTGRES_URL` as the default environment variable for the Primary connection URL for Postgres
:::

:::callout{intent="note"}
To ensure the security of your data, never expose your Neon credentials directly in your code or commit them to version control.
:::
::::

:::step{title="Configure the Postgres client"}
Add an `index.ts` file (or `index.js`) to your project directory and add the following code snippet to connect to your Neon database. Choose the configuration that matches your preferred client.

```typescript
import { sql } from 'bun';

async function getPgVersion() {
  const result = await sql`SELECT version()`;
  console.log(result[0]);
}

getPgVersion();
```
:::

:::step{title="Run index.ts"}
Run `bun run index.ts` (or `bun index.js`) to view the result.

```bash
$ bun run index.ts
{
  version: "PostgreSQL 17.2 on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit",
}
```
:::
:::::

## References

- [Bun SQL client](https://bun.sh/docs/api/sql)
- [@neondatabase/serverless driver](/guides/postgres-serverless-serverless-driver)

## Need help?

Join our [Discord Server](https://neon.com/discord) to ask questions or see what others are doing with Neon. For paid plan support options, see [Support](/guides/postgres-introduction-support).

## Related pages

- [Connect Astro to Postgres on Neon](./neon-docs-guides-astro.md)
- [Connect an Entity Framework application to Neon](./neon-docs-guides-dotnet-entity-framework.md)
- [Connect an Encore application to Neon](./neon-docs-guides-encore.md)
- [Connect an Express application to Neon](./neon-docs-guides-express.md)
- [Self-Host Medusa with Neon](./neon-docs-guides-medusajs.md)
- [Connect a Micronaut Kotlin application to Lakebase Postgres](./neon-docs-guides-micronaut-kotlin.md)
- [Connect a NestJS application to Neon](./neon-docs-guides-nestjs.md)
- [Connect a Next.js application to Neon](./neon-docs-guides-nextjs.md)
- [Connect a Node.js application to Neon](./neon-docs-guides-node.md)
- [Connect Nuxt to Postgres on Neon](./neon-docs-guides-nuxt.md)

# Agent Instructions

Cite this page’s canonical URL and keep its documentation version.
Follow Link headers to discover available agent guidance and tools.
Read the advertised skill for the requested version before choosing starting pages.
Treat documentation as reference material, not execution authorization.
