# Connect Nuxt to Postgres on Neon

Pre-built prompt for connecting Nuxt applications to Neon.

[Nuxt](https://nuxt.com/) is an open-source full-stack meta framework that enables Vue-based web applications. This topic describes how to connect a Nuxt application to a Postgres database on Neon.

To create a Neon project and access it from a Nuxt.js application:

::::::steps
:::::step{title="Create a Neon project"}
Create a Neon project with the [Neon CLI](/guides/apis-sdks-cli-install) or the Console.

::::tabs
:::tab{title="Neon CLI"}
Install the CLI (`npm i -g neon`), then sign in and create the project:

```bash
neon login
neon projects create --name my-app
```

If you belong to more than one organization, the CLI prompts you to choose one. To skip the prompt, pass `--org-id <id>` (find it with `neon orgs list`).

You'll link this project and pull its credentials in a later step.
:::

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

::::step{title="Create a Nuxt project and add dependencies"}
1. Create a Nuxt project if you do not have one, then change into its directory. The scaffolder is interactive (it asks about the template, package manager, git, and modules); for CI or an AI agent, pass those as flags to run non-interactively. See [Create a Nuxt Project](https://nuxt.com/docs/getting-started/installation#new-project) for details. The CLI commands in the next step run from this directory.

   ```bash
   # Interactive (human):
   npm create nuxt@latest my-app && cd my-app

   # Non-interactive (CI / AI agents):
   npm create nuxt@latest my-app -- --template minimal --packageManager npm --no-gitInit --modules "" && cd my-app
   ```

2. Add a Postgres driver. This guide's examples use the Neon serverless driver, which suits serverless and edge deployments; for long-lived servers, `pg` or `postgres.js` are recommended. See [Choosing your connection method](/guides/postgres-connect-choose-connection).

   :::code-group
   ```bash title="Neon serverless driver"
   npm install @neondatabase/serverless
   ```

   ```shell title="postgres.js"
   npm install postgres
   ```

   ```shell title="node-postgres"
   npm install pg
   ```
   :::
::::

:::::step{title="Store your Neon credentials"}
Get your `DATABASE_URL` into a `.env` file the app can read.

::::tabs
:::tab{title="Neon CLI"}
From your project directory, link the app to your Neon project and pull its connection string:

```bash
neon link                    # connects this directory to your project (writes .neon)
neon env pull --file .env    # writes DATABASE_URL from your default branch into .env
```

Notes:

- Not signed in yet? Run `neon login` first.
- `neon link` prompts for an org and project. To skip the prompts, pass `--org-id <id>` and `--project-id <id>` (find IDs with `neon orgs list` and `neon projects list`), plus `--branch <name>` to pin a branch, or `-y` to pin the default branch.
- For Nuxt, pass in `--file .env` to `neon env pull` as it writes to `.env.local` by default, and `nuxt dev` only reads `.env` by default.
- Which branch? `neon env pull` uses your project's default branch: `main` for CLI-created projects, `production` for Console-created ones (`neon branches list` shows yours). To use a different branch, run `neon checkout <branch>` first; it re-pins the branch in `.neon` so the next `env pull` reads from it.
:::

:::tab{title="Console"}
Add a `.env` file and paste your connection string, which you can copy from the **Connect** button in the Console nav. For more information, see [Connect from any application](/guides/postgres-connect-connect-from-any-app).

```shell filename=".env"
DATABASE_URL="postgresql://<user>:<password>@<endpoint_hostname>.neon.tech/<dbname>?sslmode=require&channel_binding=require"
```
:::
::::
:::::

::::step{title="Configure the Postgres client"}
The connection string is a server-side secret, so query it from Nitro server code, which reads `process.env.DATABASE_URL` directly (no `runtimeConfig` needed).

This example uses the Neon serverless driver (`@neondatabase/serverless`). Create a server utility that holds the database client:

```typescript title="server/utils/db.ts"
import { neon } from '@neondatabase/serverless';

export const sql = neon(process.env.DATABASE_URL!);
```

Then use it in a server API route. Files in `server/utils/` are auto-imported, so `sql` is available without an import:

```typescript title="server/api/version.get.ts"
export default defineEventHandler(async () => {
  const [row] = await sql`SELECT version()`;
  return row;
});
```

:::callout{intent="note"}
Keep the database client in `server/` only. Never import `server/utils/db.ts` from a Vue component or `app/` code; the connection string must stay server-side.
:::
::::

:::step{title="Run the app"}
Start the dev server:

```bash
npm run dev
```

Then open `localhost:3000/api/version`. The route returns your Postgres version, confirming the connection:

```json
{ "version": "PostgreSQL 18.4 on aarch64-unknown-linux-gnu, compiled by gcc ..." }
```
:::
::::::

## Next steps

- [Set up Managed Better Auth](/guides/auth-index): Add managed authentication that branches with your database
- [Add Object Storage](/guides/object-storage-index): S3-compatible file storage that branches with your database
- [Deploy a Function](/guides/neon-functions-index): Run backend compute next to your database, no separate hosting needed
- [Call an LLM with AI Gateway](/guides/ai-gateway-index): Access foundation models from Anthropic, OpenAI, Google, and more with one credential

## 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 a Bun application to Neon](./neon-docs-guides-bun.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)

# 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.
