Connect a Next.js application to Neon
Set up a Neon project in seconds and connect from a Next.js application
Next.js by Vercel is an open-source web development framework that enables React-based web applications. This topic describes how to create a Neon project and access it from a Next.js application.
To create a Neon project and access it from a Next.js application:
- Create a Neon project
- Create a Next.js project
- Add a PostgreSQL client to your app
- Add your Neon connection details
- Connect to the Neon database
Create a Neon project
If you do not have one already, create a Neon project. Save your connection details including your password. They are required when defining connection settings.
- Navigate to the Projects page in the Neon Console.
- Click New Project.
- Specify a name, a PostgreSQL version, a region, and click Create Project.
For additional information about creating projects, see Set up a project.
Create a Next.js project
Create a Next.js project if you do not have one. For instructions, see Create a Next.js App, in the Vercel documentation.
Add a PostgreSQL client to your app
Add a PostgreSQL client to your app, such as Postgres.js
. For instructions, refer to the postgres.js Getting started.
Add your Neon connection details
Add your Neon connection string to your .env
file.
DATABASE_URL=postgres://<user>:<password>@<hostname>:<port>/<database>
where:
<user>
is the database user.<password>
is the database user's password, which is provided to you when you create a project.<hostname>
the hostname of the branch's compute endpoint. The hostname has anep-
prefix and appears similar to this:ep-tight-salad-272396.us-east-2.aws.neon.tech
.<port>
is the Neon port number. The default port number is5432
.<database>
is the name of the database. The default Neon database isneondb
You can find all of the connection details listed above, except for your password, in the Connection Details widget on the Neon Dashboard. For more information, see Connect from any application. If you have misplaced your password, see Reset a password.
Connect to the Neon database
From your API handlers or server functions, connect to the Neon database with the PostgreSQL client and your Neon connection details. For example:
import postgres from 'postgres';
const sql = postgres(process.env.DATABASE_URL);
const result = await sql.unsafe(req.body);
important
Never expose your Neon credentials to the browser.
Need help?
Send a request to support@neon.tech, or join the Neon community forum.