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 and the @neondatabase/serverless driver. Use the client you prefer.
Create a project on Neon
If you do not have one already, create a project.
- Navigate to the Projects page in the console.
- Click New Project.
- Specify your project settings and click Create Project.
Create a Bun project and add dependencies
Create a Bun project and change to the newly created directory:
mkdir bun-neon-example cd bun-neon-example bun init -yNext, add project dependencies if you intend to use the Neon serverless driver. Otherwise, Bun's built-in
sqlclient is readily available.# No dependencies needed for Bun's built-in SQL clientStore your Neon credentials
Add a
.env.localfile 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.POSTGRES_URL='postgresql://[user]:[password]@[neon_hostname]/[dbname]?sslmode=require&channel_binding=require'note
Bun.sqlusesPOSTGRES_URLas the default environment variable for the Primary connection URL for Postgresimportant
To ensure the security of your data, never expose your Neon credentials directly in your code or commit them to version control.
Configure the Postgres client
Add an
index.tsfile (orindex.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.import { sql } from 'bun'; async function getPgVersion() { const result = await sql`SELECT version()`; console.log(result[0]); } getPgVersion();
References
Need help?
Join our Discord Server to ask questions or see what others are doing with Neon. For paid plan support options, see Support.








