最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

使用node.js中的knex导出数据库表架构并将数据移动到另一台计算机上

IT培训 admin 3浏览 0评论

使用node.js中的knex导出数据库表架构并将数据移动到另一台计算机上

我有一个正在运行带有postgres的Web服务A,还有另一个正在运行的node.js服务B,它从服务A复制了postgres数据。有人建议使用数据库迁移,但这是我不能做到的两个原因做。

1. Zero Downtime for the web service A, so I can't do any migration on postgres on service A.
2. postgres database have tens of Gigabytes data, migration process might eat up my CPU or block the database, which I don't want to risk it.

我正在考虑在服务B(node.js)上使用knex来获取服务A上的postgres数据库,它更像是一个简单的ETL,下面的代码可以很好地作为示例。

var knex1 = knex({
    client:'pg',
    connection:{
      database: co.database,
      host: co.host,
      user: co.user,
      password: co.password,
      port:co.port
    }
});
var sqlQuery = 'select * from abc';
var data = await knex1.select().from(knex1.raw('(' + sqlQuery + ') Wstmain'));

尽管从postgres(服务A)获取数据很好,但我的问题是在将数据插入到postgres(服务B)之前,postgres数据库(服务B)首先需要了解表架构并建立表。

所以现在我不知道如何使用该方法在数据库A上获取和导出表模式,然后使用该模式在数据库B上创建表。我找到了一个名为“ widget-knex-schema”的模块。

const migrate =  require('widget-knex-schema');
const knex =  require('knex')({config...});

const usersSchema =  {
  id: {type: 'increments', nullable: false, primary: true},
  name: {type: 'string', maxlength: 200, nullable: false},
  email: {type: 'string', maxlength: 254, nullable: false, unique: true},
  password: {type: 'string', maxlength: 254, nullable: false}
};

migrate.createTable(knex, 'users', usersSchema, true)
.then(function () {
  // success
})

它可以使用基于json的架构创建新表,但是我将如何使用knex从数据库A中导出此类基于json的架构。

回答如下:

如果需要将数据库A复制到B pg_dumppg_restore是最简单的工具https://www.postgresql/docs/9.3/app-pgdump.html`

pg_dump不会阻止其他用户访问数据库(读者或作家)。

因此,您不必担心由于进行转储而阻塞数据库。

使用node.js中的knex导出数据库表架构并将数据移动到另一台计算机上

我有一个正在运行带有postgres的Web服务A,还有另一个正在运行的node.js服务B,它从服务A复制了postgres数据。有人建议使用数据库迁移,但这是我不能做到的两个原因做。

1. Zero Downtime for the web service A, so I can't do any migration on postgres on service A.
2. postgres database have tens of Gigabytes data, migration process might eat up my CPU or block the database, which I don't want to risk it.

我正在考虑在服务B(node.js)上使用knex来获取服务A上的postgres数据库,它更像是一个简单的ETL,下面的代码可以很好地作为示例。

var knex1 = knex({
    client:'pg',
    connection:{
      database: co.database,
      host: co.host,
      user: co.user,
      password: co.password,
      port:co.port
    }
});
var sqlQuery = 'select * from abc';
var data = await knex1.select().from(knex1.raw('(' + sqlQuery + ') Wstmain'));

尽管从postgres(服务A)获取数据很好,但我的问题是在将数据插入到postgres(服务B)之前,postgres数据库(服务B)首先需要了解表架构并建立表。

所以现在我不知道如何使用该方法在数据库A上获取和导出表模式,然后使用该模式在数据库B上创建表。我找到了一个名为“ widget-knex-schema”的模块。

const migrate =  require('widget-knex-schema');
const knex =  require('knex')({config...});

const usersSchema =  {
  id: {type: 'increments', nullable: false, primary: true},
  name: {type: 'string', maxlength: 200, nullable: false},
  email: {type: 'string', maxlength: 254, nullable: false, unique: true},
  password: {type: 'string', maxlength: 254, nullable: false}
};

migrate.createTable(knex, 'users', usersSchema, true)
.then(function () {
  // success
})

它可以使用基于json的架构创建新表,但是我将如何使用knex从数据库A中导出此类基于json的架构。

回答如下:

如果需要将数据库A复制到B pg_dumppg_restore是最简单的工具https://www.postgresql/docs/9.3/app-pgdump.html`

pg_dump不会阻止其他用户访问数据库(读者或作家)。

因此,您不必担心由于进行转储而阻塞数据库。

发布评论

评论列表 (0)

  1. 暂无评论