Knex.js
Overview
Knex is a batteries-included, multi-dialect SQL query builder for Node.js supporting PostgreSQL, MySQL/MariaDB, SQLite3, MSSQL, CockroachDB, and Oracle. It builds SQL with a fluent, chainable API and adds transactions, connection pooling, streaming, and a migration/seed system. Read this when you want fine SQL control and portability without the abstraction of a full ORM.
Official sources
Install / setup
npm install knex --save
npm install pg
Core concepts
- Knex instance: configured with a
client (dialect) and connection; created once and reused.
- Query builder: chainable methods (
select, where, join, insert, update, del) that compile to SQL.
- Schema builder:
knex.schema.createTable(...) for DDL.
- Migrations & seeds: versioned schema changes and seed data run via the Knex CLI.
- Transactions:
knex.transaction(async trx => …) for atomic multi-statement work.
- Connection pooling: built-in pool managed per Knex instance.
Best practices
- Install only the driver for your database (e.g.
pg, mysql2, sqlite3) as the install guide instructs.
- Create a single Knex instance and share it; each instance owns a connection pool.
- Use migrations for all schema changes and commit them to version control.
- Use parameterized builder methods (not string concatenation) to avoid SQL injection; use
knex.raw with bindings when raw SQL is needed.
Common pitfalls
- Creating multiple Knex instances → multiple pools that can exhaust database connections; reuse one instance.
- Interpolating user input into
knex.raw strings → SQL injection; pass values as bindings (?) instead.
Examples
const knex = require('knex')({
client: 'pg',
connection: process.env.DATABASE_URL,
})
await knex('users').insert({ name: 'Ada' })
const users = await knex('users').where({ name: 'Ada' }).select('*')
Further reading
Related skills
- ../drizzle — TS-first SQL ORM that also stays close to SQL
- ../sequelize — full ORM built atop query-builder concepts
1---2name: knex3description: Flexible multi-dialect SQL query builder for Node.js with transactions, pooling, and migrations; consult when you want SQL control without a full ORM.4domain: stack5category: orm6tags: [query-builder, nodejs, sql, migrations, transactions, postgresql]7official_sources:8 - https://knexjs.org/9 - https://github.com/knex/knex10verified: 2026-06-1611metadata:12 source: https://skillmd.com/skills/gabriel7silva/knex13---1415# Knex.js1617## Overview18Knex is a batteries-included, multi-dialect SQL query builder for Node.js supporting PostgreSQL, MySQL/MariaDB, SQLite3, MSSQL, CockroachDB, and Oracle. It builds SQL with a fluent, chainable API and adds transactions, connection pooling, streaming, and a migration/seed system. Read this when you want fine SQL control and portability without the abstraction of a full ORM.1920## Official sources21- Docs: https://knexjs.org/22- Repo: https://github.com/knex/knex23- Install / guide: https://knexjs.org/guide/#node-js2425## Install / setup26```bash27npm install knex --save28npm install pg29```3031## Core concepts32- **Knex instance**: configured with a `client` (dialect) and `connection`; created once and reused.33- **Query builder**: chainable methods (`select`, `where`, `join`, `insert`, `update`, `del`) that compile to SQL.34- **Schema builder**: `knex.schema.createTable(...)` for DDL.35- **Migrations & seeds**: versioned schema changes and seed data run via the Knex CLI.36- **Transactions**: `knex.transaction(async trx => …)` for atomic multi-statement work.37- **Connection pooling**: built-in pool managed per Knex instance.3839## Best practices40- Install only the driver for your database (e.g. `pg`, `mysql2`, `sqlite3`) as the install guide instructs.41- Create a single Knex instance and share it; each instance owns a connection pool.42- Use migrations for all schema changes and commit them to version control.43- Use parameterized builder methods (not string concatenation) to avoid SQL injection; use `knex.raw` with bindings when raw SQL is needed.4445## Common pitfalls46- Creating multiple Knex instances → multiple pools that can exhaust database connections; reuse one instance.47- Interpolating user input into `knex.raw` strings → SQL injection; pass values as bindings (`?`) instead.4849## Examples50```js51const knex = require('knex')({52 client: 'pg',53 connection: process.env.DATABASE_URL,54})5556await knex('users').insert({ name: 'Ada' })57const users = await knex('users').where({ name: 'Ada' }).select('*')58```5960## Further reading61- https://knexjs.org/guide/migrations.html — migrations62- https://knexjs.org/guide/query-builder.html — query builder reference6364## Related skills65- ../drizzle — TS-first SQL ORM that also stays close to SQL66- ../sequelize — full ORM built atop query-builder concepts