Sequelize And Node.js: Initializing Multiple Databases On One Server Programmatically
When I run the server attempting to create two databases (db1 and db2), the system kicks backs this error: Possibly unhandled SequelizeBaseError: database 'db2' does not exist As
Solution 1:
You need to create the database manually before trying to access it via Sequelize - it does not create it if it does not exists. You can use a pg
module inside some initialisation script to create the DB via CREATE DATABASE
before doing any synchronisation and connection, or simply create it via postgres
CLI
const pg = require('pg');
module.exports = function(next){
var connectionData = {
user: 'postgres',
password: 'password',
host: 'localhost'
};
var databaseName = 'db2';
var connectionUri = `postgres://${user}:${password}@${host}/postgres`;
pg.connect(connectionUri, function(err, client, done) {
client.query(`CREATE DATABASE ${databaseName}`, function(error){
// here you can perform some sequelize operations after creating the database
client.end(); // disconnect client
next(); // you can call it with some parameter like Sequelize instance etc.
});
});
};
Post a Comment for "Sequelize And Node.js: Initializing Multiple Databases On One Server Programmatically"