How to configure Knex — Basic
Supplementary examples
knexfile.js excerpt
module.exports = {
development: {
client: "pg",
connection: process.env.DATABASE_URL,
migrations: { directory: "./db/migrations" },
seeds: { directory: "./db/seeds" },
},
};Shared connection module
const knex = require("knex");
const config = require("../knexfile");
module.exports = knex(config[process.env.NODE_ENV || "development"]);Course example
// Update with your config settings
module.exports = {
development: {
client: "sqlite3",
connection: {
filename: "./dev.sqlite3",
},
},
test: {
client: "postgresql",
pool: { min: 1, max: 5 },
connection: {
database: "db_test",
user: "username_test",
password: "password_test",
},
},
staging: {
client: "postgresql",
connection: {
database: "db_staging",
user: "username_staging",
password: "password_staging",
},
pool: {
min: 2,
max: 10,
},
migrations: {
tableName: "knex_migrations",
},
},
production: {
client: "postgresql",
connection: {
database: "db_production",
user: "username_production",
password: "password_production",
},
pool: {
min: 2,
max: 10,
},
migrations: {
tableName: "knex_migrations",
},
},
};
Additional references & examples
- Knex.js documentation
Query builder, migrations, seeds, and connection configuration.
- Thinkful — Node, Express, Postgres starter
Starter repo used across Knex and Express lessons.