Postgresql db从localhost到heroku

时间:2018-01-22 16:29:16

标签: postgresql heroku

我已经在本地托管了一个数据库。现在我想在Heroku中运行我的应用程序。一切顺利,但我不知道如何在Heroku上创建数据库并迁移我的所有模型。

在localhost上我有一个配置文件:

{
  "development": {
  "username": "postgres",
  "password": "admin",
  "database": "",
  "host": "127.0.0.1",
  "dialect": "postgres"
},
"test": {
"username": "postgres",
"password": null,
"database": "",
"host": "",
"dialect": ""
},
 "production": {
"username": "postgres",
"password": "admin",
"database": "",
"host": "127.0.0.1",
"dialect": "postgres"
 }
}

我的模特索引:

'use strict';

 const fs = require('fs');
 const path = require('path');
 const Sequelize = require('sequelize');
 const basename = path.basename(module.filename);
 const env = process.env.NODE_ENV || "development";
 const config = require(__dirname + '/../../config/config.json')[env];
 const db = {};


let sequelize;
  if (config.use_env_variable) {
   sequelize = new Sequelize(process.env[config.use_env_variable]);
 } else {
  sequelize = new Sequelize(
  config.database,
  config.username,
  config.password,
  config
);
 }

fs
.readdirSync(__dirname)
 .filter(
 file =>
  file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === 
'.js'
)
 .forEach(file => {
  const model = sequelize['import'](path.join(__dirname, file));
  db[model.name] = model;
 });



db.sequelize = sequelize;
db.Sequelize = Sequelize;

// FOREIGN KEY SETTING FOR USER


module.exports = db;

我有这样的脚本:

"setupDev": "createdb -U postgres shc-dev; NODE_ENV=development sequelize -U postgres db:migrate",
"setupProd": "createdb -U postgres humansocialcomputation; NODE_ENV=production sequelize -U postgres db:migrate",
"resetDev": "dropdb -U postgres shc-dev",
"resetProd": "dropdb -U postgres humansocialcomputation",
"start": "node app.js",

我的目标是在heroku上使用这个脚本,我怎样才能实现这个目标? 此外,我已经有一个来自Heroku的数据库和db_url,但是这个数据库不是空的。我怎样才能重写呢

1 个答案:

答案 0 :(得分:0)

请参阅the docs for pg:restore

例如:

heroku pg:backups:restore 'https://s3.amazonaws.com/me/items/3H0q/mydb.dump' DATABASE_URL