# Kysely (/docs/prisma-postgres/quickstart/kysely) > For the complete Prisma documentation index, see [llms.txt](https://www.prisma.io/docs/llms.txt). A markdown version of any docs page is available by appending `.md` to its URL. Get started with Kysely and Prisma Postgres by creating a type-safe SQL query builder for your database Location: Prisma Postgres > Quickstart > Kysely [Kysely](https://kysely.dev) is a type-safe TypeScript SQL query builder that provides TypeScript support and a fluent API for building SQL queries. In this guide, you'll learn how to connect Kysely to [Prisma Postgres](https://www.prisma.io/docs/postgres) and start querying your database with full type safety. ## Prerequisites [#prerequisites] * Node.js version 14 or higher * TypeScript version 4.6 or higher (5.4+ recommended for improved type inference, 5.9+ for better compilation performance) * Strict mode enabled in your `tsconfig.json` for Kysely's type safety ## 1. Create a new project [#1-create-a-new-project] Create a new directory for your project and initialize it with npm: #### bun ```bash mkdir kysely-quickstart cd kysely-quickstart bun init ``` #### pnpm ```bash mkdir kysely-quickstart cd kysely-quickstart pnpm init ``` #### yarn ```bash mkdir kysely-quickstart cd kysely-quickstart yarn init ``` #### npm ```bash mkdir kysely-quickstart cd kysely-quickstart npm init ``` Install TypeScript and initialize it: #### bun ```bash bun add --dev typescript ``` #### pnpm ```bash pnpm add --save-dev typescript ``` #### yarn ```bash yarn add --dev typescript ``` #### npm ```bash npm install --save-dev typescript ``` #### bun ```bash bunx tsc --init ``` #### pnpm ```bash pnpm tsc --init ``` #### yarn ```bash yarn tsc --init ``` #### npm ```bash npx tsc --init ``` ## 2. Configure TypeScript [#2-configure-typescript] Kysely requires TypeScript's strict mode for proper type safety. Update your `tsconfig.json` file: ```json title="tsconfig.json" { // ... "compilerOptions": { // ... "strict": true, // [!code ++] "allowImportingTsExtensions": true, // [!code ++] "noEmit": true // [!code ++] // ... } // ... } ``` > [!NOTE] > The `strict: true` setting is **required** for Kysely's type safety to work correctly. In your `package.json`, set the `type` to `module`: ```json { // ... "type": "module" // [!code ++] // ... } ``` ## 3. Create a Prisma Postgres database [#3-create-a-prisma-postgres-database] Create a Prisma Postgres database with the `create-db` CLI tool: #### bun ```bash bunx create-db ``` #### pnpm ```bash pnpm dlx create-db ``` #### yarn ```bash yarn dlx create-db ``` #### npm ```bash npx create-db ``` Then the CLI tool should output: ```bash ┌ 🚀 Creating a Prisma Postgres database │ │ Provisioning a temporary database in us-east-1... │ │ It will be automatically deleted in 24 hours, but you can claim it. │ ◇ Database created successfully! │ │ ● Database Connection │ │ │ Connection String: │ │ postgresql://hostname:password@db.prisma.io:5432/postgres?sslmode=require │ │ ◆ Claim Your Database │ │ Keep your database for free: │ │ https://create-db.prisma.io/claim?CLAIM_CODE │ │ Database will be deleted on 11/18/2025, 1:55:39 AM if not claimed. │ └ ``` Create a `.env` file and add the connection string from the output: ```text title=".env" DATABASE_URL="postgresql://hostname:password@db.prisma.io:5432/postgres?sslmode=require" ``` > [!WARNING] > **Never commit `.env` files to version control.** Add `.env` to your `.gitignore` file to keep credentials secure. The database created is temporary and will be deleted in 24 hours unless claimed. Claiming moves the database into your [Prisma Data Platform](https://console.prisma.io/?utm_source=docs\&utm_medium=content\&utm_content=%28index%29) account. Visit the claim URL from the output to keep your database. > [!NOTE] > To learn more about the `create-db` CLI tool, see the [create-db documentation](https://www.prisma.io/docs/postgres/npx-create-db). ## 4. Install dependencies [#4-install-dependencies] Install Kysely and the PostgreSQL driver: #### bun ```bash bun add kysely pg dotenv ``` #### pnpm ```bash pnpm add kysely pg dotenv ``` #### yarn ```bash yarn add kysely pg dotenv ``` #### npm ```bash npm install kysely pg dotenv ``` #### bun ```bash bun add --dev @types/pg tsx ``` #### pnpm ```bash pnpm add --save-dev @types/pg tsx ``` #### yarn ```bash yarn add --dev @types/pg tsx ``` #### npm ```bash npm install --save-dev @types/pg tsx ``` **Package breakdown:** * `kysely`: The type-safe SQL query builder * `pg`: PostgreSQL driver for Node.js (required by Kysely's PostgresDialect) * `dotenv`: Loads environment variables from `.env` file * `@types/pg`: TypeScript type definitions for the pg driver * `tsx`: TypeScript execution engine for running `.ts` files directly ## 5. Define database types [#5-define-database-types] Create a `src/types.ts` file to define your database schema types: ```typescript title="src/types.ts" import type { Generated } from "kysely"; export interface Database { users: UsersTable; } export interface UsersTable { id: Generated; email: string; name: string | null; } ``` ## 6. Configure database connection [#6-configure-database-connection] Create a `src/database.ts` file to instantiate Kysely with your Prisma Postgres connection: ```typescript title="src/database.ts" import "dotenv/config"; import type { Database } from "./types.ts"; import { Pool } from "pg"; import { Kysely, PostgresDialect } from "kysely"; // Parse DATABASE_URL into connection parameters function parseConnectionString(url: string) { const parsed = new URL(url); return { host: parsed.hostname, port: parseInt(parsed.port), user: parsed.username, password: parsed.password, database: parsed.pathname.slice(1), // Remove leading '/' }; } const connectionParams = parseConnectionString(process.env.DATABASE_URL!); const dialect = new PostgresDialect({ pool: new Pool({ ...connectionParams, ssl: true, max: 10, }), }); // Database interface is passed to Kysely's constructor, and from now on, Kysely // knows your database structure. // Dialect is passed to Kysely's constructor, and from now on, Kysely knows how // to communicate with your database. export const db = new Kysely({ dialect, }); ``` ## 7. Run queries [#7-run-queries] Create a `src/script.ts` file: ```typescript title="src/script.ts" import { db } from "./database.ts"; async function main() { // Create the users table await db.schema .createTable("users") .ifNotExists() .addColumn("id", "serial", (col) => col.primaryKey()) .addColumn("email", "varchar(255)", (col) => col.notNull().unique()) .addColumn("name", "varchar(255)") .execute(); // Insert a user const user = await db .insertInto("users") .values({ email: "alice@prisma.io", name: "Alice", }) .returningAll() .executeTakeFirstOrThrow(); console.log("Created user:", user); // Query all users const users = await db.selectFrom("users").selectAll().execute(); console.log("All users:", users); } main() .then(async () => { await db.destroy(); }) .catch(async (error) => { console.error("Error:", error); await db.destroy(); process.exit(1); }); ``` Run the script: #### bun ```bash bunx tsx src/script.ts ``` #### pnpm ```bash pnpm dlx tsx src/script.ts ``` #### yarn ```bash yarn dlx tsx src/script.ts ``` #### npm ```bash npx tsx src/script.ts ``` You should receive the following output: ```bash Created user: { id: 1, email: 'alice@prisma.io', name: 'Alice' } All users: [ { id: 1, email: 'alice@prisma.io', name: 'Alice' } ] ``` ## Next steps [#next-steps] Kysely is now connected to Prisma Postgres. For schemas, migrations, and more complex queries, see the [Kysely documentation](https://kysely.dev/docs/intro). > [!NOTE] > Deploy to Compute > > To run this app in the cloud, deploy it to [Prisma Compute](https://www.prisma.io/docs/compute), which runs your app next to your Prisma Postgres database. Follow [Deploy your first app](https://www.prisma.io/docs/prisma-compute/deploy). ## Related pages - [`Drizzle ORM`](https://www.prisma.io/docs/prisma-postgres/quickstart/drizzle-orm): Get started with Drizzle ORM and Prisma Postgres - [`Prisma ORM`](https://www.prisma.io/docs/prisma-postgres/quickstart/prisma-orm): Create a Prisma ORM app with Prisma Postgres. - [`TypeORM`](https://www.prisma.io/docs/prisma-postgres/quickstart/typeorm): Get started with TypeORM and Prisma Postgres by connecting your TypeScript ORM to a managed PostgreSQL database