I'm not sure about PostgreSQL, but I hit the same problem with MySQL. I discovered you can use knex to connect without selecting a database, create the database with raw SQL, then re-connect selecting the new database.
Here is a stand-alone script that creates a new database with a single-column table:
var conn = {
host: '127.0.0.1',
user: 'user',
password: 'pass',
charset: 'utf8',
};
// connect without database selected
var knex = require('knex')({ client: 'mysql', connection: conn });
knex.raw('CREATE DATABASE my_database').then(function () {
knex.destroy();
// connect with database selected
conn.database = 'my_database';
knex = require('knex')({ client: 'mysql', connection: conn });
knex.schema
.createTable('my_table', function (table) {
table.string('my_field');
})
.then(function () {
knex.destroy();
});
});
This works (well enough for me for now) but I'm interested to hear of other solutions.