Connect Your App to Neon Postgres
Connect to Neon from server-side code and test PostgreSQL schema changes on an isolated database branch.
Neon is serverless PostgreSQL with separate compute and storage. Use it when your app needs Postgres and you want a separate database branch for testing schema changes. The connection string selects the branch your code will query, so keep development and production values separate.
Connect from server-side code
Create or choose a database in Neon
Open Neon and choose the project and database branch for the environment you’re setting up. A Neon entry in Studio doesn’t create the database or provide its connection string.
Set the connection string
Copy the connection string for that branch into the server-side DATABASE_URL environment variable. Use the pooled connection string for application traffic when your driver supports it, and use a direct connection for schema migrations. Treat both strings as credentials.
Run a query from your server
Use @neondatabase/serverless from code that can read the server environment. Run a query against a table you expect on that branch; the example below assumes your schema already defines a users table with an active column.
import { neon } from "@neondatabase/serverless";
const databaseUrl = process.env.DATABASE_URL;
if (!databaseUrl) throw new Error("Set DATABASE_URL on the server");
const sql = neon(databaseUrl);
export async function getActiveUsers() {
return sql`SELECT id, name, email FROM users WHERE active = true`;
}Test migrations on a database branch
Create an isolated Neon branch for a feature and, if useful, give it the same name as the Git branch. Matching names helps you identify the pair, but it doesn’t change DATABASE_URL. Point your development or test environment at the Neon branch before running a migration. Verify the schema and run the app against that branch before applying the reviewed migration to production.
Plan for compute and extensions
Neon compute can scale up under load and scale to zero when idle. Account for a resume when testing the first query after an idle period. Neon supports PostgreSQL extensions such as pgvector, uuid-ossp, and pg_trgm. If your schema depends on one, include it in the migration you test on the branch.
Check the branch when a query fails
If the example can’t find users, verify that the running app loads the connection for the branch where you applied the migration. If the query runs but returns no rows, confirm that branch has test users marked active. Don’t point a development app at production to get test data.