Sequelize’s DatabaseError typically means your Node.js application’s database connection or query execution failed at the database level.

Here are the most common culprits and how to fix them:

1. Connection Pool Exhaustion

  • What broke: Your Node.js app tried to get a database connection from Sequelize’s pool, but all available connections were already in use and the pool’s max limit was reached. The database server didn’t even see the request because Sequelize couldn’t hand it a connection.
  • Diagnosis: Check your Sequelize configuration for pool.max. If you don’t see it, it defaults to 5. Then, monitor your application’s active connections. Tools like pg_stat_activity (for PostgreSQL) or SHOW PROCESSLIST (for MySQL) can show you how many connections your database server has. If the number of connections from your app consistently hovers around pool.max, this is likely the issue. You can also use libraries like sequelize-pool-stats to get insights directly from your Node.js app.
  • Fix:
    • Increase pool.max: In your Sequelize new Sequelize(...) constructor, add or increase pool.max. For example:
      const sequelize = new Sequelize('database', 'username', 'password', {
        // ... other options
        pool: {
          max: 15, // Increase from default 5
          min: 0,
          acquire: 30000,
          idle: 10000
        }
      });
      
      This allows more simultaneous connections from your app.
    • Optimize Queries: Long-running queries tie up connections. Profile your database queries to find and optimize slow ones.
    • Release Connections: Ensure you’re not holding onto connections longer than necessary. If you’re using raw SQL or manually managing transactions, make sure to call connection.release() when done.
  • Why it works: A larger pool allows more concurrent requests to be served by the database.

2. Network Connectivity Issues / Firewall Blocks

  • What broke: Sequelize attempted to establish a connection to the database server, but the network path was interrupted, or a firewall blocked the connection attempt. The database server never received the connection request.
  • Diagnosis:
    • From your application server, try to telnet <db_host> <db_port> (e.g., telnet db.example.com 5432). If it times out or says "Connection refused," it’s a network or firewall issue.
    • Check your cloud provider’s security groups or your on-premises firewall rules to ensure traffic is allowed from your app server’s IP to the database server’s IP on the correct port (e.g., 5432 for PostgreSQL, 3306 for MySQL).
  • Fix:
    • Update Firewall Rules: Allow inbound traffic on the database port from your application server’s IP address.
    • Check Network Infrastructure: Ensure there are no routing issues or network outages between your app and the database.
  • Why it works: Establishes the fundamental network path required for any communication.

3. Invalid Database Credentials or Permissions

  • What broke: Sequelize successfully connected to the database server, but the provided username/password were incorrect, or the user lacked the necessary privileges to perform the requested operation (e.g., SELECT on a table they don’t have access to). The database server rejected the connection or the specific query.
  • Diagnosis:
    • Double-check the username, password, database, host, and port in your Sequelize connection string or configuration object.
    • Try connecting to the database directly from your application server using a command-line client with the same credentials (e.g., psql -h db.example.com -U myuser -d mydatabase or mysql -h db.example.com -u myuser -p mydatabase).
    • If direct connection works, check the database user’s grants. For example, in PostgreSQL: \du to list users and their roles, and \dp your_table_name to check table permissions.
  • Fix:
    • Correct Credentials: Update the username, password, database, host, and port in your Sequelize configuration.
    • Grant Permissions: In your database, grant the necessary privileges to the user. For example, in PostgreSQL:
      GRANT SELECT, INSERT, UPDATE, DELETE ON your_table_name TO your_user;
      -- Or for all tables in a schema:
      GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO your_user;
      
      In MySQL:
      GRANT SELECT, INSERT, UPDATE, DELETE ON your_database.your_table_name TO 'your_user'@'your_host';
      FLUSH PRIVILEGES;
      
  • Why it works: Ensures the application is authenticating correctly and has the minimum required permissions to interact with the database.

4. Database Server Not Running or Unreachable

  • What broke: Sequelize attempted to connect, but the database server process itself was not running, or it was running but on an IP address or port that is not accessible.
  • Diagnosis:
    • Check the status of your database server process (e.g., sudo systemctl status postgresql or sudo systemctl status mysql).
    • Ping the database host from your application server.
    • Try connecting using a database client from the application server.
  • Fix:
    • Start the Database Service: If the service is stopped, start it (e.g., sudo systemctl start postgresql).
    • Check Database Configuration: Ensure the database is configured to listen on the expected network interface and port.
  • Why it works: The database must be running and accessible for any connection to be made.

5. Incorrect host or port Configuration

  • What broke: Sequelize tried to connect to a database server at a specified host and port, but no database server was listening there. This could be a typo in your configuration, or the database might be running on a different port than expected (e.g., default 5432 for PostgreSQL, 3306 for MySQL).
  • Diagnosis:
    • Verify the host and port values in your Sequelize new Sequelize(...) constructor or connection string.
    • Use telnet <db_host> <db_port> from your app server to confirm a service is listening on that specific host and port.
  • Fix:
    • Correct host and port: Update your Sequelize configuration to match the actual location of your database server. For example:
      const sequelize = new Sequelize('database', 'username', 'password', {
        host: 'rds-prod-db.example.com', // Corrected host
        port: 5432, // Corrected or default port
        dialect: 'postgres'
      });
      
  • Why it works: Directs the connection attempt to the correct network endpoint where the database is actively listening.

6. Database Server Overloaded (Resource Limits)

  • What broke: The database server is running but is overwhelmed with requests, has run out of memory, or its CPU is maxed out. It’s unable to accept new connections or process incoming queries efficiently, leading to timeouts or connection rejections reported by Sequelize.
  • Diagnosis:
    • Monitor your database server’s CPU, memory, and I/O usage. Tools like htop, iotop, or cloud provider monitoring dashboards are essential.
    • Check database-specific performance metrics (e.g., PostgreSQL’s pg_stat_activity for active queries, pg_locks for blocking).
    • Look for error logs on the database server itself.
  • Fix:
    • Scale Database Resources: Increase CPU, RAM, or disk I/O for your database server.
    • Optimize Queries: Identify and optimize slow or resource-intensive queries that are consuming database capacity.
    • Reduce Load: Implement caching, optimize application logic to make fewer requests, or distribute read load to replica databases.
  • Why it works: Ensures the database server has sufficient resources to handle incoming requests and maintain responsiveness.

The next error you might encounter after fixing these is a SequelizeUniqueConstraintError if your data violates a unique index, or a SequelizeValidationError if your data doesn’t meet model validation rules.

Want structured learning?

Take the full Nodejs course →