sequelize: Error: Please install 'pg' module manually

I’m in the process of switching to Yarnpkg from npm, and i hit this issue. Was working fine before when i was using npm, but using yarn i get this error.

Error: Please install 'pg' module manually
    at new ConnectionManager (/home/app/kerberos/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:27:13)
    at new PostgresDialect (/home/app/kerberos/node_modules/sequelize/lib/dialects/postgres/index.js:12:28)
    at new Sequelize (/home/app/kerberos/node_modules/sequelize/lib/sequelize.js:233:18)

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 36 (3 by maintainers)

Commits related to this issue

Most upvoted comments

Looks like this has to do with where you have the modules installed.

I was running a globally installed sequelize, and got this error. When I ran ./node_modules/.bin/sequelize from the node_modules folder it ran fine.

So if you’re running sequelize from the globally installed node folder, you’ll need to also have pg installed globally

node v7.2.1, sequelize 3.27.0, pg 6.1.1 returns the same problem when running migrations. Deleted node_modules, reinstalled sequelize and sequelize-cli globally, etc… still stuck with this error. Solved this by installing pg globally (npm install -g pg). Is that a new requirement ?

a workaround that just works…

import * as pg from 'pg';
import { Sequelize } from 'sequelize';

const sequelize = new Sequelize('postgres://admin:admin@localhost:5432/mydb', {
  dialectModule: pg
});

webpack.config.js

module.exports = {
 output: {
    libraryTarget: 'commonjs'
  },
  externals: [
    { pg: { commonjs: 'pg' } }
  ],
}

This issue has been fixed in v5 but is back in v6 😬

I was using webpack and I got it working by following changes:

  • npm install:
npm install --save-dev noop2
npm install pg pg-hstore
  • webpack.config.js
module.exports = {
  // .... all your webpack config here after that add resolve
  resolve: {
    alias: {
      'pg-native': 'noop2',
      tedious: 'noop2',
      sqlite3: 'noop2',
      mysql2: 'noop2',
    },
  },
};
  • code:
import * as pg from 'pg';
import { Sequelize } from 'sequelize';

const sequelize = new Sequelize(
  process.env.DB_NAME, //database
  process.env.DB_USER, //username
  process.env.DB_PASSWORD, //password
  {
    host: process.env.DB_HOST,
    dialect: 'postgres',
    dialectModule: pg,
  });

(async () => {
  try {
    await sequelize.authenticate();
    console.log('Connection has been established successfully.');
  } catch (error) {
    console.error('Unable to connect to the database:', error);
  }
})();

Looks like this has to do with where you have the modules installed. If you install this globally rather than locally, you can uninstall using the following command: npm uninstall -g sequalize

Ran into this issue while using NextJS and the serverless-nextjs-plugin. The solution for me was the following:

Original code with error:

import { Sequelize, Model } from 'sequelize';

export const sequelize = new Sequelize(process.env.DB_NAME, process.env.DB_USERNAME, process.env.DB_PASSWORD, {
    host: process.env.DB_HOST,
    port: 5432,
    dialect: 'postgres'
});

Fix (notice pg import and dialectModule specification):

import { Sequelize, Model } from 'sequelize';
import pg from 'pg';

export const sequelize = new Sequelize(process.env.DB_NAME, process.env.DB_USERNAME, process.env.DB_PASSWORD, {
    host: process.env.DB_HOST,
    port: 5432,
    dialect: 'postgres',
    dialectModule: pg
});

The same but without webpack and etc.:

import * as pg from 'pg';

const sequelize = new Sequelize({
    ...
    dialectModule: pg
});

npm install --save-dev noop2

package.json:

{
    dependencies: {
        "pg-native": "file:./node_modules/noop2"
    }
}

Hey coders.

I had this same problem and lost a long time to figure out the solution.

My project is based on lambda serverless from AWS.

In my serverless.yml file i was using serverless-bundle to optimize my package.

After a long time of testing i figured out that this pack need some configurations when using sequelize.

https://www.npmjs.com/package/serverless-bundle#sequelize

About documentation:

To use the Sequelize package along with pg, you’ll need to ignore it from Webpack and using the dialectModule option. Read more here.

In your serverless.yml:

custom:
  bundle:
    ignorePackages:
      - pg-native

And in your Lambda code:

const sequelize = new Sequelize(
  process.env.DB_NAME,
  process.env.DB_USERNAME,
  process.env.DB_PASSWORD,
  {
    host: process.env.DB_HOST,
    dialect: process.env.DB_DIALECT,
    dialectModule: pg
  }
);

Wehre pg is your pg module import like:

import pg from "pg";

I hope it helps someone!

Enjoy coding.

This issue can be resolve either by installing pg package globally. Or by installing sequelize and sequelize-cli packages locally. With local installation you can run sequelize like ./node_modules/.bin/sequelize db:migrate

I followed the proposed solutions but didn’t work:

custom:
  webpack:
    includeModules:
      forceInclude:
        - pg
        - pg-hstore

I ended switching to mysql because I even tried the solution that I’m copy/pasting from https://github.com/sequelize/sequelize/issues/9489#issuecomment-493304014 and couldn’t make Postgres to work.

// this works with mysql
import Sequelize from 'sequelize';
import mysql2 from 'mysql2'; // Needed to fix sequelize issues with WebPack

const sequelize = new Sequelize(
  process.env.DB_NAME,
  process.env.DB_USER,
  process.env.DB_PASSWORD,
  {
    dialect: 'mysql',
    dialectModule: mysql2, // Needed to fix sequelize issues with WebPack
    host: process.env.DB_HOST,
    port: process.env.DB_PORT
  }
)

export async function connectToDatabase() {
  console.log('Trying to connect via sequelize')
  await sequelize.sync()
  await sequelize.authenticate()
  console.log('=> Created a new connection.')

  // Do something 
}

Adding the below code solved my problem

custom:
  webpackIncludeModules:
    forceInclude:
      - pg

yes i have

"pg": "6.1.0"
"sequelize": "3.27.0"

node v7.2.1, sequelize 3.27.0, pg 6.1.1 returns the same problem when running migrations. Deleted node_modules, reinstalled sequelize and sequelize-cli globally, etc… still stuck with this error. Solved this by installing pg globally (npm install -g pg). Is that a new requirement ?

Thanks, npm install -g pg fixed this issue.

I noticed that this is only an issue when i have installed with yarn --production or having NODE_ENV=production, so most probably a yarn issue… but still wierd as i have no module devDependencies that i would link to sequelize

I have developed an Express app that is working correctly on my local machine. Now, I am attempting to host it on Vercel, but I am encountering the following error. image

When I check the logs, it indicates that there is an issue. " Error: Please install sqlite3 package manually at ConnectionManager._loadDialectModule (/var/task/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:55:15) at new ConnectionManager (/var/task/node_modules/sequelize/lib/dialects/sqlite/connection-manager.js:18:21) at new SqliteDialect (/var/task/node_modules/sequelize/lib/dialects/sqlite/index.js:13:30) at new Sequelize (/var/task/node_modules/sequelize/lib/sequelize.js:194:20) at Object.<anonymous> (/var/task/services/database.js:3:19) at Module._compile (node:internal/modules/cjs/loader:1256:14) at Module._extensions…js (node:internal/modules/cjs/loader:1310:10) at Module.load (node:internal/modules/cjs/loader:1119:32) at Module._load (node:internal/modules/cjs/loader:960:12) at Ze.e.<computed>._module.Module._load (/var/task/___vc/__launcher/__launcher.js:14:1964) Error: Please install sqlite3 package manually at ConnectionManager._loadDialectModule (/var/task/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:55:15) at new ConnectionManager (/var/task/node_modules/sequelize/lib/dialects/sqlite/connection-manager.js:18:21) at new SqliteDialect (/var/task/node_modules/sequelize/lib/dialects/sqlite/index.js:13:30) at new Sequelize (/var/task/node_modules/sequelize/lib/sequelize.js:194:20) at Object.<anonymous> (/var/task/services/database.js:3:19) at Module._compile (node:internal/modules/cjs/loader:1256:14) at Module._extensions…js (node:internal/modules/cjs/loader:1310:10) at Module.load (node:internal/modules/cjs/loader:1119:32) at Module._load (node:internal/modules/cjs/loader:960:12) at Ze.e.<computed>._module.Module._load (/var/task/___vc/__launcher/__launcher.js:14:1964) Error: Runtime exited with error: exit status 1 Runtime.ExitError

" I think i have installed sqlite3 already as i am using the following package.json file package.json file " { “name”: “funrun4”, “version”: “0.0.0”, “private”: true, “main”: “/index.js”, “engines”: { “node”: “18.x” }, “scripts”: { “start”: “node ./bin/www”, “dev”: “nodemon ./bin/www -e js,ejs,html,css” }, “dependencies”: { “app-module-path”: “^2.2.0”, “cookie-parser”: “~1.4.4”, “debug”: “~2.6.9”, “dotenv”: “^16.3.1”, “ejs”: “^3.1.9”, “express”: “^4.18.2”, “http-errors”: “~1.6.3”, “morgan”: “~1.9.1”, “sequelize”: “^6.33.0”, “sqlite3”: “^5.1.6” }, “devDependencies”: { “nodemon”: “^3.0.1” } }

"

I am not sure how to resolve this problem. Please provide guidance on how to correct it.

I found a fix that worked for me! If you have webpack configured it might help. First, install webpack-node-externals

yarn add -D webpack-node-externals

then in your webpack require it

const nodeExternals = require('webpack-node-externals')

and finally in your config :

externals: [nodeExternals()]