Getting Started with Vercel Postgres
You can get started with writing to and reading data from Vercel Postgres by following this quickstart guide.Vercel Postgres is available on Hobby and Pro plans
This page will walk you through setting up basic API routes that use Vercel Postgres.
If you'd prefer to deploy a template, head to the Templates section.
To follow this quickstart, you'll need the following:
- An existing Vercel Project
- The Vercel Postgres package
pnpm i @vercel/postgres
- The latest version of Vercel CLI
pnpm i -g vercel@latest
In this quickstart, you'll use Vercel Postgres SDK to create a database of you and your friends' pets.
You'll learn to:
- Create a Postgres database that's connected to one of your projects
- Create and manage the database in your dashboard on Vercel
- Populate the database using Vercel's Postgres SDK
- Send the contents of a PostgreSQL table as a response when someone visits one of your API routes
If you're looking to use @vercel/postgres
and connect to a local Postgres
instance (example: Docker), see the Local Development
guide.
To get started, let's create a database.
- In your dashboard on Vercel, create or select the project you want to work with
- Select the Storage tab, then select the Connect Store button
- Select Postgres
- Enter a database name. It can only contain alphanumeric letters (including "_" and "-") and can't exceed 32 characters. We'll choose
pets_postgres_db
- Select a region. If your project has Edge or Serverless Functions, choose an option near their regions for faster responses
- Select Create and Continue
- In the next view, change nothing and select Connect
You now have an empty PostgreSQL database in your selected region!
To connect to your database with Vercel Postgres SDK, you need a few credentials. By connecting your database to a project, those credentials are generated and made available as environment variables:
POSTGRES_URL
POSTGRES_PRISMA_URL
POSTGRES_URL_NON_POOLING
POSTGRES_USER
POSTGRES_HOST
POSTGRES_PASSWORD
POSTGRES_DATABASE
You can see them by navigating to the Settings tab in your project and selecting the Environment Variables panel.
When you created your Postgres database and connected it to your project, your API URL and credentials were added to the project as environment variables automatically. You'll need to pull them into your local environment to access your Postgres database.
In your terminal, run:
vercel env pull .env.development.local
Next, let's create a
Pets
table in your database that will hold very basic information about your pets.-
From the Storage tab, select the Postgres database you just created
-
Select the Query tab in the Data section
-
Paste the following
SQL
queryCREATE TABLE Pets ( Name varchar(255), Owner varchar(255) );
-
Select Run Query
You can see your database by searching for
pets
in the Browse tab.-
Now that our table exists, let's add some data.
The following API route:
- Adds a new row to your PostgreSQL database with both the
ownerName
andpetName
fields taken from the query string in the URL when the route is visited - Returns the contents of the entire table
Add this file to your project:
app/api/add-pet/route.tsimport { sql } from '@vercel/postgres'; import { NextResponse } from 'next/server'; export async function GET(request: Request) { const { searchParams } = new URL(request.url); const petName = searchParams.get('petName'); const ownerName = searchParams.get('ownerName'); try { if (!petName || !ownerName) throw new Error('Pet and owner names required'); await sql`INSERT INTO Pets (Name, Owner) VALUES (${petName}, ${ownerName});`; } catch (error) { return NextResponse.json({ error }, { status: 500 }); } const pets = await sql`SELECT * FROM Pets;`; return NextResponse.json({ pets }, { status: 200 }); }
Isn't it a security risk to embed text into SQL queries? – Not in this case. Vercel sanitizes all queries sent to your Vercel Postgres database before executing them. The above code does not expose you to SQL injections.
To send data to your database, you must visit the route:
- Run
next dev
to start your app locally
- Add data to the query params when you visit your route. Here's an example URL that adds a pet named
Fluffy
with an owner namedJohn
to your database:
You should see something like this in your browser:
{ pets: { command: "SELECT", fields: [ { columnID: 1, dataTypeID: 1043, dataTypeModifier: 259, dataTypeSize: -1, format: "text", name: "name", tableID: 12345, }, { columnID: 2, dataTypeID: 1043, dataTypeModifier: 259, dataTypeSize: -1, format: "text", name: "owner", tableID: 12345, }, ], rowCount: 1, rows: [ { name: "Fluffy", owner: "John", }, ], viaNeonFetch: true, }, };
And in your dashboard, under the Data tab when you view your database, you can search for the Pets table to see the pets and owners you've added.
- Adds a new row to your PostgreSQL database with both the
You now have API routes that create a table in your PostgreSQL database and add data to it!
To learn more about using Vercel Postgres in your projects, read our SDK reference.
RAG with Vercel AI SDK
A retrieval-augmented generation chatbot built with Next.js, the Vercel AI SDK, Drizzle ORM, and PostgreSQL.
Semantic Image Search
An open-source AI semantic image search app template built with Next.js, Vercel AI SDK, OpenAI, Vercel Postgres, Vercel Blob and Vercel KV.
Vercel Postgres + Kysely Next.js Starter
Simple Next.js template that uses Vercel Postgres as the database and Kysely as the query builder.
Was this helpful?