{"id":18466,"library":"knex-db-manager","title":"knex-db-manager","description":"Collection of administrative database operations for knex, supporting PostgreSQL, MySQL, and SQLite3. Version 0.7.0 (latest) provides APIs for creating, dropping, copying, truncating databases, and managing database owners via a privileged superuser connection. Helps with test setup, initializing fresh databases, and truncating between tests. Requires knex 0.x and the appropriate database driver. Differentiates from raw knex by offering high-level admin functions like copyDb and truncateDb.","status":"active","version":"0.7.0","language":"javascript","source_language":"en","source_url":"https://github.com/Vincit/knex-db-manager","tags":["javascript","knex","sql","admin","management","postgresql","mysql"],"install":[{"cmd":"npm install knex-db-manager","lang":"bash","label":"npm"},{"cmd":"yarn add knex-db-manager","lang":"bash","label":"yarn"},{"cmd":"pnpm add knex-db-manager","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"peer dependency for database query building and connection management","package":"knex","optional":false},{"reason":"string escaping for PostgreSQL, automatically installed but may be needed explicitly","package":"pg-escape","optional":true}],"imports":[{"note":"CJS require is fine; ESM named import also works.","wrong":"const knexDbManager = require('knex-db-manager')","symbol":"databaseManagerFactory","correct":"import { databaseManagerFactory } from 'knex-db-manager'"},{"note":"Module has a default export that is the factory function itself. CJS users can use require directly.","wrong":"const { default: knexDbManager } = require('knex-db-manager')","symbol":"default","correct":"import knexDbManager from 'knex-db-manager'"},{"note":"DatabaseManager is only exported as a type, not a value.","wrong":"import { DatabaseManager } from 'knex-db-manager'","symbol":"DatabaseManager","correct":"import type { DatabaseManager } from 'knex-db-manager'"}],"quickstart":{"code":"const { databaseManagerFactory } = require('knex-db-manager');\n\nconst config = {\n  knex: {\n    client: 'postgres',\n    connection: {\n      host: 'localhost',\n      user: 'dbowneruser',\n      password: process.env.DB_PASSWORD || '',\n      database: 'myapp_dev',\n    },\n    pool: { min: 0, max: 10 },\n    migrations: { directory: './migrations' },\n  },\n  dbManager: {\n    superUser: 'postgres',\n    superPassword: process.env.SUPER_PASSWORD || '',\n    collate: ['en_US.UTF-8'],\n    populatePathPattern: 'data/**/*.js',\n  },\n};\n\nconst dbManager = databaseManagerFactory(config);\n\ndbManager.createDbOwnerIfNotExist()\n  .then(() => dbManager.createDb())\n  .then(() => dbManager.truncateDb(['migrations']))\n  .catch(err => console.error(err));","lang":"javascript","description":"Shows initialization and basic usage: create db owner, create database, truncate tables except migrations."},"warnings":[{"fix":"Add dbManager.close() after all operations to ensure clean exit.","message":"DatabaseManager may not close all connections; superuser connection can remain open. Call dbManager.close() to close the knex instance and superuser connection.","severity":"gotcha","affected_versions":">=0.5.0"},{"fix":"Avoid copyDb for very large databases; use native tools.","message":"When using copyDb with large databases, the operation copies on the server side and can cause high disk usage; consider using pg_dump/pg_restore instead.","severity":"gotcha","affected_versions":"*"},{"fix":"Always provide dbManager.superUser and dbManager.superPassword.","message":"The superUser and superPassword fields are required; omitting them results in an error. This was not always enforced in early versions.","severity":"breaking","affected_versions":">=0.6.0"},{"fix":"Remove populatePathPattern and run seeds via knex commands.","message":"The populatePathPattern option is deprecated; use knex seed functionality instead.","severity":"deprecated","affected_versions":">=0.6.0"},{"fix":"Do not use copyDb with MySQL; implement your own copy logic.","message":"The copyDb method does not work with MySQL; it throws an error if used.","severity":"gotcha","affected_versions":"*"},{"fix":"Ensure all tables are listed; use ignoreTables for tables with dependencies.","message":"truncateDb may fail if there are foreign key constraints; it disables triggers temporarily but some databases may require CASCADE.","severity":"gotcha","affected_versions":"*"},{"fix":"Set collate to an array of collations that the database supports; can be empty array if unsure.","message":"The collate option is database-specific; using wrong collation can cause errors on createDb.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Ensure config.knex includes client: 'postgres' (or mysql/sqlite3).","cause":"knex config object not passed correctly; missing client property.","error":"Error: knex: Required configuration option 'client' is missing."},{"fix":"Run: npm install pg-escape","cause":"The package requires pg-escape for PostgreSQL, but it is not installed.","error":"Error: Cannot find module 'pg-escape'"},{"fix":"Add superUser and superPassword to dbManager configuration object.","cause":"dbManager config missing superUser or superPassword fields.","error":"DBManager: superUser and superPassword must be specified"},{"fix":"Use: const dbManager = databaseManagerFactory(config); then call dbManager.createDb().","cause":"The module was imported incorrectly; databaseManagerFactory returns an object with methods.","error":"dbManager.createDb is not a function"},{"fix":"Call await dbManager.createDbOwnerIfNotExist() before createDb().","cause":"The db owner user has not been created yet; use createDbOwnerIfNotExist() first.","error":"error: role \"dbowneruser\" does not exist"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}