title: Sequelize and Node.js: Initializing Multiple Databases On One Server Programmatically
body: <p>When I run the server attempting to create two databases (db1 and db2), the system kicks backs this error:</p>
  
  <pre><code>Possibly unhandled SequelizeBaseError: database "db2" does not exist
  </code></pre>
  
  <p>As a reference, there is similar stackoverflow question on this topic <a href="https://stackoverflow.com/questions/37078970/sequelize-using-multiple-databases">here</a>, however, the author of the solution does not cover the solution to how the server.js file is setup. You will notice I have structured my index.js file similar to their answer. </p>
  
  <p>My models/index.js file and server run and executes scripts properly, yet the second database does not work at all and does not even get initialized. </p>
  
  <p><strong>Can someone provide a solution for the server.js file to accurately initialize two databases in this one server?</strong> </p>
  
  <p>The following is the partial code from the models/index.js and server.js files. In the server.js file, I am using .sync to initialize the databases. </p>
  
  <p>server.js</p>
  
  <pre><code>    [...] 
      //sync's sequelize DB and tables                                                                                                                                                                     
      db['db1'].sequelize.sync(function(err){});
      db['db2'].sequelize.sync(function(err){});
  </code></pre>
  
  <p>models/index.js</p>
  
  <pre><code>    var databasesArray = ['db1', 'db2']
      var databasesObj = {
          database: {
              db1: {
                  DBName: 'db1',
                  User: user,
                  Password: password,
                  Config: config,
              },
              db2: {
                  DBName: 'db2',
                  User: user,
                  Password: password,
                  Config: config,
              }
          }
      } // EOF databaseObj                                                                                                                                                                     
  
      for(var i = 0; i &lt; databasesArray.length; ++i) {
          var databasePointerToOBJ = databasesArray[i];
          var database = databasesObj.database[databasePointerToOBJ]
          if(database.DBName == 'db1'){
              var sq = new Sequelize(database.DBName, user, password, config)
              db['db1'] = {
                  Sequelize: Sequelize,
                  sequelize: sq,
                  Table1: sq.import(__dirname + '/...')
              }
          }else if(database.DBName == 'db2'){
              var sq = new Sequelize(database.DBName, user, password, config)
              db['db2'] = {
                  Sequelize: Sequelize,
                  sequelize: sq,
                  Table1: sq.import(__dirname + '/...')
              }
          }
      }
  module.exports = db;
  </code></pre>
  
  <p><strong>---  EDIT  ---</strong></p>
  
  <p>The author of the solution was correct. In order for a new database to be created, it must be created prior to being sync'd. As piotrbienias rightly notes, adding the code for adding a new DB in a your initialization script would be the best option. Piotrbienias solution is <strong>.js</strong> option. The code that worked for me is a <strong>.sh</strong> option and is as following:</p>
  
  <pre><code>PG_HOST=localhost
  PG_PORT=5432
  PG_DB=databaseName
  PG_USER=ubuntu
  PG_PASS='EnterPassword'
  
  sudo -u postgres createdb -U postgres -O $PG_USER $PG_DB
  </code></pre>