我有一个托管在 Heroku 上的 Node.js/Rails3 应用程序。当它在我的本地机器或远程生产机器上运行时,rails 部分在 PostgreSQL 和 SQLite3 之间无缝切换。
在本地,rails 框架连接到 config/databases.yml
中定义的 SQLite3,当我推送到 Heorku 时,部署脚本会使用其生产设置覆盖它。
我的 node.js 脚本没有 Heroku 可以 Hook 并确保我在生产环境中使用正确数据库的框架。
如何让我的 node.js 脚本“正常工作”,就像我的 Rails 应用程序在开发和生产环境之间无缝移动一样?
最佳答案
Heroku 公开了一个数据库 URL,您可以通过 DATABASE_URL
环境变量连接到该 URL。这是来自 Heroku Dev Center docs 的相关部分.
Using a Postgres Database
To add a PostgreSQL database to your app, run this command:
$ heroku addons:add shared-database
This sets the
DATABASE_URL
environment variable. Add thepostgres
NPM module to your dependencies:"dependencies": { ... "pg": "0.5.4" }
And use the module to connect to
DATABASE_URL
from somewhere in your code:var pg = require('pg'); pg.connect(process.env.DATABASE_URL, function(err, client) { var query = client.query('SELECT * FROM your_table'); query.on('row', function(row) { console.log(JSON.stringify(row)); }); });
关于ruby-on-rails - Heroku 上的 Node.js : PostgreSQL on prod, SQLite3 on dev?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7452444/