{"record":{"id":"9a000656226df307","repo":"n8n-io/n8n","slug":"postgres-package-has-not-been-found-installed-try","errorCode":null,"errorMessage":"Postgres package has not been found installed. Try to install it: npm install pg --save","messagePattern":"Postgres package has not been found installed\\. Try to install it: npm install pg --save","errorType":"exception","errorClass":"DriverPackageNotInstalledError","httpStatus":null,"severity":"critical","filePath":"packages/@n8n/typeorm/src/driver/postgres/PostgresDriver.ts","lineNumber":1324,"sourceCode":"\t\t\t\t`To use streams you should install pg-query-stream package. Please run npm i pg-query-stream --save command.`,\n\t\t\t);\n\t\t}\n\t}\n\n\t// -------------------------------------------------------------------------\n\t// Protected Methods\n\t// -------------------------------------------------------------------------\n\n\t/**\n\t * If driver dependency is not given explicitly, then try to load it via \"require\".\n\t */\n\tprotected loadDependencies(): void {\n\t\ttry {\n\t\t\tconst postgres = this.options.driver || require('pg');\n\t\t\tthis.postgres = postgres;\n\t\t} catch (e) {\n\t\t\t// todo: better error for browser env\n\t\t\tthrow new DriverPackageNotInstalledError('Postgres', 'pg');\n\t\t}\n\t}\n\n\t/**\n\t * Creates a new connection pool for a given database credentials.\n\t */\n\tprotected async createPool(\n\t\toptions: PostgresConnectionOptions,\n\t\tcredentials: PostgresConnectionCredentialsOptions,\n\t): Promise<any> {\n\t\tconst { logger } = this.connection;\n\t\tcredentials = Object.assign({}, credentials);\n\n\t\t// build connection options for the driver\n\t\t// See: https://github.com/brianc/node-postgres/tree/master/packages/pg-pool#create\n\t\tconst connectionOptions: pg.PoolConfig = {\n\t\t\tconnectionString: credentials.url,\n\t\t\thost: credentials.host,","sourceCodeStart":1306,"sourceCodeEnd":1342,"githubUrl":"https://github.com/n8n-io/n8n/blob/5ac6606e81f67bb9534255570cd4e86fd8101eee/packages/@n8n/typeorm/src/driver/postgres/PostgresDriver.ts#L1306-L1342","documentation":"DriverPackageNotInstalledError('Postgres', 'pg') is thrown from PostgresDriver.loadDependencies when both this.options.driver is unset and require('pg') throws. The driver cannot operate without the pg client library; this fires during connect() before any real DB work.","triggerScenarios":"DB_TYPE=postgres (or options.type='postgres') is configured but the 'pg' npm package is not installed in the running process; or pg is installed in a different workspace/node_modules than the one the process resolves from.","commonSituations":"Custom Docker image that did not include pg; pnpm hoisting / strict-node-loader setup that hides pg from the typeorm package; an upgrade that moved pg to optionalDependencies and it got pruned.","solutions":["Install pg in the package that runs the DataSource: 'npm install pg'.","Pass the driver explicitly via options.driver = pg if installing globally is not possible (this short-circuits the require).","For monorepos, ensure pg is a real dependency of the runtime workspace, not just of a sibling.","Rebuild/reinstall node_modules if a prune step removed it."],"exampleFix":"// before\n// DB_TYPE=postgres but pg not installed -> DriverPackageNotInstalledError\n// after\nnpm install pg\n","handlingStrategy":"validation","validationCode":"// At startup, probe that pg is resolvable when postgres is selected.\nif (config.dbType === 'postgres') {\n  try { require.resolve('pg'); } catch {\n    throw new Error('pg package not installed; run npm install pg');\n  }\n}\n","typeGuard":null,"tryCatchPattern":"import { DriverPackageNotInstalledError } from '@n8n/typeorm';\ntry {\n  await dataSource.initialize();\n} catch (e) {\n  if (e instanceof DriverPackageNotInstalledError && e.args?.[0] === 'Postgres') {\n    failStartup('Postgres driver needs the pg package; run npm install pg');\n  }\n  throw e;\n}\n","preventionTips":["Pin pg as a hard dependency in the package that runs the DataSource.","Run a startup probe for the driver package matching DB_TYPE.","In monorepos, ensure pg is hoisted to where the runtime resolves it.","Audit image-pruning scripts so they do not strip pg."],"tags":["typeorm","postgres","dependencies","database","startup"],"backgroundTag":null,"analyzedSha":"5ac6606e81f67bb9534255570cd4e86fd8101eee","analyzedAt":"2026-08-12T05:26:35.080Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}