AI Workshop: learn to build apps with AI →
PostgreSQL: Connect to Postgres local vs Vercel Postgres with Kysely

Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.


I switched a codebase from local Postgres database to Vercel Postgres, which comes with its own optimized package.

I used this to set up Kysely:

import {
  Kysely,
  PostgresDialect,
} from 'kysely'

import pg from 'pg'

const POSTGRES_URL = //...from env

const dialect = new PostgresDialect({
  pool: new pg.Pool({
    connectionString: POSTGRES_URL,
    max: 10,
  }),
})

export const db = new Kysely({
  dialect
})

With Vercel Postgres, I used:

import { createKysely } from '@vercel/postgres-kysely'

const POSTGRES_URL = //...from env

export const db = createKysely({
  connectionString: POSTGRES_URL
})

The db now already knows how to look up the environment variable POSTGRES_URL

Lessons in this unit:

0: Introduction
1: Introduction to PostgreSQL
2: How to install PostgreSQL on macOS
3: How to create a PostgreSQL database
4: Navigating Databases in PostgreSQL
5: PostgreSQL User Permissions
6: Tables and Data Types
7: Where to host a PostgreSQL database
8: PostgreSQL vs MySQL
9: Troubleshooting PostgreSQL
10: Vercel Postgres, no transactions?
11: Set up Lucia Auth for local Postgres DB vs Vercel Postgres
12: ▶︎ Connect to Postgres local vs Vercel Postgres with Kysely