我已经从市场创建了postgress服务(通过cf create-service),我想在我的node.js应用程序中使用它。(我能够在本地测试它有效) 我有两个问题
1.我尝试了以下内容,应用程序无法启动并且在我得到的日志中 这是我对env varible的价值,我在这里错过了什么?
这是代码:
OK i've tried the following and the application doesnt able to start and in the log I got
这是我对env varible的价值,我在这里缺少什么?
OUT env variable host: 10.0.97.139
OUT port: 34807
OUT user: qmxgvfybloierztm
OUT password: mlofvwfsxmf7bqjr
OUT database: r8n13yjyql7hwrgc
OUT url: postgres://qmxgvfybloierztm:mlofvwfsxmf7bqjr@10.0.97.135:34607/r8n13yjyql7hwrgc
OUT start create table
OUT ERROR: connect: Error: connect ECONNREFUSED 10.0.97.135:5432
(function(){
if (null == process.env.VCAP_SERVICES) {
var url = 'postgress://localhost:27017/local';
}
else {
var vcap_services = JSON.parse(process.env.VCAP_SERVICES);
console.log("postgress URL: ", url);
};
var DBWrapper = require('node-dbi').DBWrapper;
var dbConnectionConfig = {
uri: vcap_services['postgresql'][0].credentials.uri,
host: vcap_services['postgresql'][0].credentials.hostname,
port: vcap_services['postgresql'][0].credentials.port,
database: vcap_services['postgresql'][0].credentials.dbname,
user: vcap_services['postgresql'][0].credentials.username,
password: vcap_services['postgresql'][0].credentials.password
};
var dbWrapper = new DBWrapper('pg', dbConnectionConfig);
dbWrapper.connect();
console.log("start create table");
dbWrapper.query("CREATE TABLE IF NOT EXISTS TAB1 ( firstname TEXT primary key, lastname varchar(20) )", function (err, results) {
if (err) {
console.log("error while inserting data " + err);
} else {
console.log("success to insert data: ");
}
});
})();
更新(在Jerome回答之后......)这是我的所有代码!!!
"use strict";
var express = require("express");
var path = require("path");
var app = express();
var port = process.env.PORT || 3000;
app.listen(port, function () {
(function () {
var DBWrapper = require('node-dbi').DBWrapper;
var vcap_services = JSON.loparse(process.env.VCAP_SERVICES);
var pgconf = vcap_services['postgresql'][0].credentials;
var dbConnectionConfig = {
dsn: pgconf.uri
};
var dbWrapper = new DBWrapper('pg', dbConnectionConfig);
dbWrapper.connect();
console.log("env variable host: " + pgconf.hostname)
console.log("port: " + pgconf.port);
console.log("user: " + pgconf.user);
console.log("password: " + pgconf.password);
console.log("database: " + pgconf.database);
console.log("url: " + pgconf.uri);
console.log("start create table");
dbWrapper.query("CREATE TABLE IF NOT EXISTS USER ( firstname TEXT primary key, lastname varchar(20) )", function (err, results) {
if (err) {
console.log("error while inserting data " + err);
} else {
console.log("success to insert data: ");
}
});
var data =
{
firstname: 'John5',
lastname: 'Foo4444'
};
//insert data
dbWrapper.insert('USER', data, function (err, data) {
if (err) {
console.log("error to insert data: " + err);
// John has been inserted in our table, with its properties safely escaped
} else {
console.log("test" + data);
}
});
//read data
dbWrapper.fetchAll("SELECT * FROM USER", null, function (err, result) {
if (!err) {
console.log("Data came back from the DB.", result);
} else {
console.log("DB returned an error: %s", err);
}
dbWrapper.close(function (close_err) {
if (close_err) {
console.log("Error while disconnecting: %s", close_err);
}
});
});
})();
});
现在我收到了这个错误
2016-07-26T11:55:49.69+0300 [App/0] OUT env variable host: undefined
2016-07-26T11:55:49.69+0300 [App/0] OUT port: 35058
2016-07-26T11:55:49.69+0300 [App/0] OUT user: undefined
2016-07-26T11:55:49.69+0300 [App/0] OUT password: hvevfgpjjtyqpr1d
2016-07-26T11:55:49.69+0300 [App/0] OUT database: undefined
2016-07-26T11:55:49.69+0300 [App/0] OUT url: postgres://fakttklwkxtfprgv:hvevfgpjjtyqpr1d@10.0.97.140:35058/ispkmc5psgdrwj4e
2016-07-26T11:55:49.69+0300 [App/0] OUT start create table
2016-07-26T11:55:49.69+0300 [App/0] OUT ERROR: connect: Error: getaddrinfo ENOTFOUND undefined undefined:5432
答案 0 :(得分:4)
根据code of node-dbi,pg适配器将连接URL构建为
var cfg = this._connectionParams;
if (connectionParams["dsn"]) {
this._pgUrl = connectionParams["dsn"];
} else {
this._pgUrl = 'pg://'+cfg.user+':'+cfg.password+'@'+cfg.host+'/'+cfg.database;
}
this._dbClient = new pg.Client(this._pgUrl);
查看错误消息,
ERROR: connect: Error: connect ECONNREFUSED 10.0.97.135:5432
您的代码正在尝试连接到默认的postgres端口5432,这暗示了端口未正确转发到pg的事实。 node-dbi源代码不使用cfg.port变量,所以这可以解释你的错误,因为如果我们相信' url'那么pg库永远不会被赋予你的postgres实例正在监听的34607端口。您的帖子中显示的ENV变量。
另请注意,node-dbi在存在时更喜欢dsn:
this._pgUrl = connectionParams["dsn"];
您似乎在ENV中使用名称' url'。
很难从外面给你一个确切的解决方案,但我相信它应该与
一起使用var DBWrapper = require('node-dbi').DBWrapper;
var pgconf = vcap_services['postgresql'][0].credentials;
var dbConnectionConfig = {
dsn: pgconf.url
};
var dbWrapper = new DBWrapper('pg', dbConnectionConfig);
dbWrapper.connect();
<强>更新强>
给定的解决方案导致错误
OUT ERROR: connect: Error: getaddrinfo ENOTFOUND undefined undefined:5432
通常表示没有给出dsn或者没有pg
正确地分析dsn。这导致我看到node-dbi
使用的是pg
的旧版本。所以我们不应该尝试将连接参数用作字符串。
您确认您使用的node-dbi
版本是0.7.1吗?
我刚刚安装了node-dbi
。它给了我0.7.1,在那里我可以阅读
var cfg = this._connectionParams;
this._pgUrl = 'pg://' + cfg.user + ':' + cfg.password + '@' + cfg.host + '/' + cfg.database;
this._dbClient = new pg.Client(this._pgUrl);
所以这段代码与我们在github上看到的不一致。此版本不处理dsn。
这可以通过https://github.com/DrBenton/Node-DBI/commits/master确认。 0.7.1版本的日期为2014年1月,postgres dsn处理时间为2015年1月,因此尚未发布...
您的日志存在差异,我不明白为什么它声明主机未定义,所以我会直接使用uri绕过它,因为uri似乎是正确的。
了解node-dbi
如何构建pg连接字符串,您可以尝试
var url = require('url');
var DBWrapper = require('node-dbi').DBWrapper;
var pgconf = vcap_services['postgresql'][0].credentials;
console.log(pgconf.uri);
var parts = url.parse(pgconf.uri);
var dbConnectionConfig = {
user: parts.auth.split(':')[0],
password: parts.auth.split(':')[1],
host: parts.host,
database: parts.pathname.substr(1)
};
var dbWrapper = new DBWrapper('pg', dbConnectionConfig);
dbWrapper.connect();
一旦你开始工作,就可能想知道为什么node-dbi
看起来如此&#34;脆弱&#34;例如,如果您不想直接使用sequelize
,请使用其他数据库包装器pg
。
然后它应该与
一起使用var sequelize = new Sequelize(pgconf.uri, {})