[Fixed] Express knex migrations do not create database tables

Issue

I am using knex with express as a backend ..
This is the knexfile

module.exports = {
  development: {
    client: 'postgresql',
    connection: {
      host: process.env.DB_HOST,
      port: process.env.DB_PORT,
      database: process.env.DB_NAME,
      user: process.env.DB_USER,
      password: process.env.DB_PASSWORD
    },
    pool: {
      min: 2,
      max: 10
    },
    migrations: {
      tableName: 'knex_migrations',
      directory: './src/knex/migrations'
    },
    seeds: {
      directory: './src/knex/seeds'
    }
  },

  production: {
    client: 'postgresql',
    connection: {
      database: 'my_db',
      user: 'username',
      password: 'password'
    },
    pool: {
      min: 2,
      max: 10
    },
    migrations: {
      tableName: 'knex_migrations'
    }
  }
};

I could connect to the database and I have tested that from my express .. and I can as well create migrations using npx knex

and the cli migrate successfully only on the terminal give green successful messages… But still, the DB has zero tables. no migrations table or any tables .. i have even delete the database and recreate another one … it seems like the cli does not connect with the actual Postgres server ..
Any Idea.

a user migration

exports.up = function (knex, Promise) {
  return knex.schema.createTable('users', function (table) {
    table.increments();
    table.string('email').notNull();
    table.string('password').notNull();
    table.timestamp('created_at').defaultTo(knex.fn.now());
    table.timestamp('updated_at').defaultTo(knex.fn.now());
    table.timestamp('deleted_at');
  });
};

exports.down = function (knex, Promise) {
  return knex.schema.dropTable('users');
};

Solution

the client for postgresql should be ‘pg’ and not ‘postgresql’
try changing it in both development and production

Leave a Reply

(*) Required, Your email will not be published