尝试将我的node.js连接到Heroku PostgreSQL数据库。 Heroku Postgres教程之后 [英] Trying to connect my node.js to Heroku PostgreSQL database. Following Heroku Postgres tutorial

查看:181
本文介绍了尝试将我的node.js连接到Heroku PostgreSQL数据库。 Heroku Postgres教程之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



https://devcenter.heroku.com/articles/getting-started-with-nodejs



>

https://devcenter.heroku.com/articles / heroku-postgresql



我有'hello world'应用程序工作。但是当我添加node.js代码连接到postgreSQL时,我收到了一个错误。



我的package.json

  {
name :node-example,
version:0.0.1,
dependencies:{
pg:2.x,
express:3.1.x
},
engines:{
node:0.10.x,
npm:1.2.x
}
}

我的web.js

  var express = require(express); 
var app = express();
app.use(express.logger());

app.get('/',function(request,response){
response.send('Hello World!');
});

var port = process.env.PORT || 5000;
app.listen(port,function(){
console.log(Listening on+ port);
});

var pg = require('pg');
$ b pg.connect(process.env.DATABASE_URL,function(err,client,done){
client.query('SELECT * FROM your_table',function(err,result){
done();
if(err)return console.error(err);
console.log(result.rows);
});
});

我的Heroku postgres数据库运行良好,我可以直接使用它连接到$ / b

  heroku pg:psql 

这是我的日志:

  2013-09-29T13:13:34.777156 + 00:00 heroku [web.1]:状态从开始变为变化
2013-09-29T13:13:34.784018 + 00:00 app [web.1]:
2013-09-29T13:13:34.787193 + 00:00 app [web .1]:events.js:72
2013-09-29T13:13:34.787469 + 00:00 app [web.1]:throw er; //未处理'错误'事件
2013-09-29T13:13:34.787642 + 00:00 app [web.1]:^
2013-09-29T13:13:34.790791 + 00:00 app [web.1]:error:relationjunkdoes not exist
2013-09-29T13:13:34.790791 + 00:00 app [web.1]:at Connection.parseE(/ app / node_modules / pg /lib/connection.js:546:11)
2013-09-29T13:13:34.790791 + 00:00 app [web.1]:在Connection.parseMessage(/ app / node_modules / pg / lib / connection .js:375:17)
2013-09-29T13:13:34.790791 + 00:00 app [web.1]:at null。< anonymous> (/app/node_modules/pg/lib/connection.js:92:20)
2013-09-29T13:13:34.790791 + 00:00 app [web.1]:Socket.EventEmitter.emit(events .js:95:17)
2013-09-29T13:13:34.790791 + 00:00 app [web.1]:在Socket。< anonymous> (_stream_readable.js:746:14)
2013-09-29T13:13:34.790791 + 00:00 app [web.1]:在Socket.EventEmitter.emit(events.js:92:17)
2013-09-29T13:13:34.790791 + 00:00 app [web.1]:at emitReadable_(_stream_readable.js:408:10)
2013-09-29T13:13:34.790791 + 00:00 app [web.1]:at emitReadable(_stream_readable.js:404:5)
2013-09-29T13:13:34.790791 + 00:00 app [web.1]:at readableAddChunk(_stream_readable.js:165 :9)
2013-09-29T13:13:34.790968 + 00:00 app [web.1]:在Socket.Readable.push(_stream_readable.js:127:10)
2013-09- 29T13:13:36.511975 + 00:00 heroku [web.1]:进程已退出,状态为8
2013-09-29T13:13:36.527681 + 00:00 heroku [web.1]:状态从up改为坠毁
2013-09-29T13:21:22 + 00:00 heroku [slug-compiler]:Slug编译开始
2013-09-29T13:21:38 + 00:00 heroku [slug-compiler ]:Slug编辑完成
2013-09-29T13:21:39.239935 + 00:00 heroku [web.1]:状态从崩溃转换为开始
2013-09-29T13:21:40.589773 + 00 :00 heroku [ web.1]:使用命令`node web.js`启动程序
2013-09-29T13:21:41.345806 + 00:00 app [web.1]:听20977
2013-09- 29T13:21:41.368323 + 00:00 app [web.1]:{[错误:关系your_table不存在]
2013-09-29T13:21:41.368323 + 00:00 app [web.1 ]:长度:101,
2013-09-29T13:21:41.368323 + 00:00 app [web.1]:detail:undefined,
2013-09-29T13:21:41.368323 + 00: 00应用[web.1]:严重性:'错误',
2013-09-29T13:21:41.368323 + 00:00应用[web.1]:提示:undefined,
2013-09- 29T13:21:41.368323 + 00:00 app [web.1]:position:'15',
2013-09-29T13:21:41.368323 + 00:00 app [web.1]:code:'42P01 ',
2013-09-29T13:21:41.368323 + 00:00 app [web.1]:name:'error',
2013-09-29T13:21:41.368323 + 00:00 app [web.1]:internalPosition:undefined,
2013-09-29T13:21:41.368512 + 00:00 app [web.1]:其中:undefined,
2013-09-29T13:21: 41.368512 + 00:00 app [web.1]:file:'parse_relation.c',
2013-09-29T13:21:41.368512 + 00:00 app [web.1]:li ne:'864',
2013-09-29T13:21:41.368323 + 00:00 app [web.1]:internalQuery:undefined,
2013-09-29T13:21:41.368512 + 00: 00 app [web.1]:例程:'parserOpenTable'}
2013-09-29T13:21:41.938926 + 00:00 heroku [web.1]:状态从开始改为
2013- 09-29T13:21:38.600520 + 00:00 heroku [api]:通过*********@gmail.com部署95a0a35
2013-09-29T13:21:38.625733 + 00:00 heroku [api]:发布v17由*******@gmail.com创建
2013-09-29T13:22:08.383050 + 00:00 heroku [router]:at = info method = GET path = / host = pure-lake-7106.herokuapp.com fwd =58.7.243.156dyno = web.1 connect = 3ms service = 6ms status = 200 bytes = 12
2013-09-29T13:22:08.383327 + 00 :00 app [web.1]: - - - [Sun,29 Sep 2013 13:22:08 GMT]GET / HTTP / 1.1200 12 - Mozilla / 5.0(X11; Linux x86_64)AppleWebKit / 537.36(KHTML,如Gecko)Ubuntu Chromium / 28.0.1500.71 Chrome / 28.0.1500.71 Safari / 537.36
2013-09-29T13:22:10.046808 + 00:00 app [web.1] : - - - [Sun,29 Sep 2013 13:22:10 GMT]GET /favicon.ico HTTP / 1.1404 - - Mozilla / 5.0(X11; Linux x86_64)AppleWebKit / 537.36(KHTML,像Gecko )Ubuntu Chromium / 28.0.1500.71 Chrome / 28.0.1500.71 Safari / 537.36
2013-09-29T13:22:10.049179 + 00:00 heroku [router]:at = info method = GET path = / favicon.ico host = pure-lake-7106.herokuapp.com fwd =58.7.243.156dyno = web.1 connect = 1ms service = 3ms status = 404 bytes = 34
2013-09-29T13:29:40 + 00 :00 heroku [slug-compiler]:Slug编译开始
2013-09-29T13:30:07.484077 + 00:00 heroku [api]:通过xxxxxxxxxxx@gmail.com部署a2cc795
2013-09- 29T13:30:07.515481 + 00:00 heroku [api]:发布v18由xxxxxxxxx@gmail.com创建
2013-09-29T13:30:07 + 00:00 heroku [slug-compiler]:Slug编译完成
2013-09-29T13:30:08.016355 + 00:00 heroku [web.1]:状态从最高变为
2013-09-29T13:30:10.099473 + 00 :00 heroku [web.1]:使用SIGTERM
停止所有进程2013-09-29T13:30:11.008770 + 00:00 app [web.1]:在47344
上收听2013-09-29T13 :30:11.065531 + 00:00 app [web.1]:name:'error',
2013-09-29T13:30:11.065531 + 00:00 app [web.1]:length:101,
2013-09-29T13:30:11.065531 + 00:00 app [web.1]:严重程度:'错误',
2013-09-29T13:30:11.065531 + 00:00 app [web。 1]:code:'42P01',
2013-09-29T13:30:11.065531 + 00:00 app [web.1]:detail:undefined,
2013-09-29T13:30:11.065531 +00:00 app [web.1]:position:'15',
2013-09-29T13:30:11.065531 + 00:00 app [web.1]:{[error:relationyour_tabledoes不存在]
2013-09-29T13:30:11.065531 + 00:00 app [web.1]:internalPosition:undefined,
2013-09-29T13:30:11.065531 + 00:00 app [ web.1]:internalQuery:undefined,
2013-09-29T13:30:11.065840 + 00:00 app [web.1]:wh ere:undefined,
2013-09-29T13:30:11.065840 + 00:00 app [web.1]:file:'parse_relation.c',
2013-09-29T13:30:11.065840+ 00:00 app [web.1]:line:'864',
2013-09-29T13:30:11.065840 + 00:00 app [web.1]:例程:'parserOpenTable'}
2013-09-29T13:30:11.065531 + 00:00 app [web.1]:hint:undefined,
2013-09-29T13:30:11.482704 + 00:00 heroku [web.1]:状态已更改从开始到结束
2013-09-29T13:30:11.651117 + 00:00 heroku [web.1]:进程退出状态143
2013-09-29T13:30:17.729604 + 00:00 [web.1]: - - - [Sun,29 Sep 2013 13:30:17 GMT]GET / HTTP / 1.1200 12 - Mozilla / 5.0(X11; Linux x86_64)AppleWebKit / 537.36(KHTML,如Gecko)Ubuntu Chromium / 28.0.1500.71 Chrome / 28.0.1500.71 Safari / 537.36
2013-09-29T13:30:19.361615 + 00:00 heroku [router]:at = info method = GET path = / favicon.ico host = pure-lake-7106.herokuapp.com fwd =58.7.243.156dyno = web.1 connect = 1ms service = 3ms status = 404 bytes = 34
2013-09-29T13:30:19.364457 + 00:00 app [web.1]: - - - [Sun,29 Sep 2013 13:30:19 GMT]GET /favicon.ico HTTP / 1.1404 - - Mozilla / 5.0(X11; Linux x86_64)AppleWebKit / 537.36(KHTML,如Gecko)Ubuntu Chromium / 28.0.1500.71 Chrome / 28.0.1500.71 Safari / 537.36
2013-09-29T13:30:17.728815 + 00 :00 heroku [router]:at = info method = GET path = / host = pure-lake-7106.herokuapp.com fwd =58.7.243.156dyno = web.1 connect = 1ms service = 19ms status = 200 bytes = 12
2013-09-29T13:32:28 + 00:00 heroku [slug-compiler]:Slug编译开始
2013-09-29T13:32:43.338858 + 00:00 heroku [api]:部署d4cf2ba by xxxxxxxx@gmail.com
2013-09-29T13:32:43.359317 + 00:00 heroku [api]:发布v19 cre ated by xxxxxxxx@gmail.com
2013-09-29T13:32:43 + 00:00 heroku [slug-compiler]:Slug汇编完成
2013-09-29T13:32:43.746015 + 00: 00 heroku [web.1]:状态从最初变为
2013-09-29T13:32:45.354842 + 00:00 heroku [web.1]:使用命令`node web.js`启动进程
2013-09-29T13:32:46.0​​98651 + 00:00 app [web.1]:收听37156
2013-09-29T13:32:47.127328 + 00:00 app [web.1]:{ [错误:关系your_table不存在]
2013-09-29T13:32:47.127328 + 00:00 app [web.1]:code:'42P01',
2013-09-29T13 :32:47.127328 + 00:00 app [web.1]:name:'error',
2013-09-29T13:32:47.127328 + 00:00 app [web.1]:length:101,
2013-09-29T13:32:47.127328 + 00:00 app [web.1]:hint:undefined,
2013-09-29T13:32:47.127328 + 00:00 app [web.1] :position:'15',
2013-09-29T13:32:47.127328 + 00:00 app [web.1]:severity:'ERROR',
2013-09-29T13:32:47.127328 +00:00 app [web.1]:detail:undefined,
2013-09-29T13:32:47.127561 + 00:00 app [web.1] :其中:undefined,
2013-09-29T13:32:47.127561 + 00:00 app [web.1]:例程:'parserOpenTable'}
2013-09-29T13:32:47.127328 + 00 :00 app [web.1]:internalPosition:undefined,
2013-09-29T13:32:47.127328 + 00:00 app [web.1]:internalQuery:undefined,
2013-09-29T13 :32:47.127561 + 00:00 app [web.1]:file:'parse_relation.c',
2013-09-29T13:32:47.127561 + 00:00 app [web.1]:line:' 864',
2013-09-29T13:32:47.197293 + 00:00 heroku [web.1]:状态从开始变为
2013-09-29T13:32:50.505267 + 00:00 heroku [web.1]:使用SIGTERM
停止所有进程2013-09-29T13:32:53.246120 + 00:00 heroku [web.1]:进程已退出,状态为143
2013-09-29T14 :39:50.833246 + 00:00 heroku [web.1]:怠速
2013-09-29T14:39:52.828292 + 00:00 heroku [web.1]:停止所有使用SIGTERM
2013的进程-09-29T14:39:54.545662 + 00:00 heroku [web.1]:进程已退出,状态为143
2013-09-29T14:39:54.559151 + 00:00 heroku [web.1]:状态已更改从上到下

当我做工头时,我得到了这个错误

  p $ foreman start 
12:39:41 web.1 |以pid开头13983
12:39:41 web.1 |听5000美元b $ b 12:39:41 web.1 | /home/roland/github/heroku_app/web.js:18
12:39:41 web.1 | client.query('SELECT * FROM your_table',function(err,result){
12:39:41 web.1 | ^
12:39:41 web.1 | TypeError:无法调用方法'query'of null
12:39:41 web.1 | at /home/roland/github/heroku_app/web.js:18:10
12:39:41 web.1 | at / home / roland / github / heroku_app / node_modules / pg / lib / pool.js:54:25
12:39:41 web.1 | at / home / roland / github / heroku_app / node_modules / pg / node_modules / generic-pool / lib / generic-pool.js:271:11
12:39:41 web.1 | at /home/roland/github/heroku_app/node_modules/pg/lib/pool.js:27: 26
12:39:41 web.1 | at null。< anonymous>(/home/roland/github/heroku_app/node_modules/pg/lib/client.js:169:9)
12 :39:41 web.1 | at EventEmitter.emit(events.js:95:17)
12:39:41 web.1 | at null。< anonymous>(/ home / roland / github / heroku_app /node_modules/pg/lib/connection.js:97:12)
12:39:41 web.1 | at Socket.EventEmitter.emit(events.js:95:17)
12:39 :41 web.1 | at Socket。< an onymous> (_stream_readable.js:746:14)
12:39:41 web.1 |在Socket.EventEmitter.emit(events.js:92:17)
12:39:41 web.1 |用代码8
退出12:39:41 system |发送SIGTERM到所有进程
SIGTERM收到

编辑我的console.logs;

  var express = require(express); 
var app = express();
app.use(express.logger());

app.get('/',function(request,response){
response.send('Hello World!');
console.log(hello roland );
});

var port = process.env.PORT || 5000;
app.listen(port,function(){
console.log(Listening on+ port);
});

var pg = require('pg');


pg.connect(process.env.DATABASE_URL,function(err,client,done){
console.log(err +!!!!!!!!! ();
client.query('SELECT * FROM your_table',function(err,result){
done();
if(err)return console。错误(错误);
console.log(result.rows);
});
});


解决方案

这是Heroku的问题。他们告诉你在pg.connect中使用的process.env.DATABASE_URL变量不起作用。



一个简单的

  console.log(process.env.DATABASE_URL); 

会显示这个变量未定义。



<直到Heroku提供了一个修复程序,您可以将连接URL硬编码为pg.connect()的第一个参数。



要找到您的凭据,您可以通过 http://heroku.com 添加到您应用的PostgreSQL加载项连接设置。



新的pg.connect方法看起来像

  var connectionString =postgres:// * USERNAME *:* PASSWORD * @ * HOST *:* PORT * / * DATABASE *

pg.connect(connectionString,function(err,client,done){
client.query(' SELECT * FROM your_table',function(err,result){
done();
if(err)return console.error(err);
console.log(result.rows);
});
});


I'm following these two Heroku tutorials:

https://devcenter.heroku.com/articles/getting-started-with-nodejs

and

https://devcenter.heroku.com/articles/heroku-postgresql

I have the 'hello world' app working. But I am getting an error when I add the node.js code to connect to postgreSQL.

My package.json

{
  "name": "node-example",
  "version": "0.0.1",
  "dependencies": {
    "pg": "2.x",
    "express": "3.1.x"
  },
  "engines": {
    "node": "0.10.x",
    "npm": "1.2.x"
  }
}

My web.js

var express = require("express");
var app = express();
app.use(express.logger());

app.get('/', function(request, response) {
  response.send('Hello World!');
});

var port = process.env.PORT || 5000;
app.listen(port, function() {
  console.log("Listening on " + port);
});

var pg = require('pg');

pg.connect(process.env.DATABASE_URL, function(err, client, done) {
  client.query('SELECT * FROM your_table', function(err, result) {
    done();
    if(err) return console.error(err);
    console.log(result.rows);
  });
});

My Heroku postgres database is working well and I can connect to it directly with

heroku pg:psql

Here are my logs:

2013-09-29T13:13:34.777156+00:00 heroku[web.1]: State changed from starting to up
2013-09-29T13:13:34.784018+00:00 app[web.1]: 
2013-09-29T13:13:34.787193+00:00 app[web.1]: events.js:72
2013-09-29T13:13:34.787469+00:00 app[web.1]:         throw er; // Unhandled 'error' event
2013-09-29T13:13:34.787642+00:00 app[web.1]:               ^
2013-09-29T13:13:34.790791+00:00 app[web.1]: error: relation "junk" does not exist
2013-09-29T13:13:34.790791+00:00 app[web.1]:     at Connection.parseE (/app/node_modules/pg/lib/connection.js:546:11)
2013-09-29T13:13:34.790791+00:00 app[web.1]:     at Connection.parseMessage (/app/node_modules/pg/lib/connection.js:375:17)
2013-09-29T13:13:34.790791+00:00 app[web.1]:     at null.<anonymous> (/app/node_modules/pg/lib/connection.js:92:20)
2013-09-29T13:13:34.790791+00:00 app[web.1]:     at Socket.EventEmitter.emit (events.js:95:17)
2013-09-29T13:13:34.790791+00:00 app[web.1]:     at Socket.<anonymous> (_stream_readable.js:746:14)
2013-09-29T13:13:34.790791+00:00 app[web.1]:     at Socket.EventEmitter.emit (events.js:92:17)
2013-09-29T13:13:34.790791+00:00 app[web.1]:     at emitReadable_ (_stream_readable.js:408:10)
2013-09-29T13:13:34.790791+00:00 app[web.1]:     at emitReadable (_stream_readable.js:404:5)
2013-09-29T13:13:34.790791+00:00 app[web.1]:     at readableAddChunk (_stream_readable.js:165:9)
2013-09-29T13:13:34.790968+00:00 app[web.1]:     at Socket.Readable.push (_stream_readable.js:127:10)
2013-09-29T13:13:36.511975+00:00 heroku[web.1]: Process exited with status 8
2013-09-29T13:13:36.527681+00:00 heroku[web.1]: State changed from up to crashed
2013-09-29T13:21:22+00:00 heroku[slug-compiler]: Slug compilation started
2013-09-29T13:21:38+00:00 heroku[slug-compiler]: Slug compilation finished
2013-09-29T13:21:39.239935+00:00 heroku[web.1]: State changed from crashed to starting
2013-09-29T13:21:40.589773+00:00 heroku[web.1]: Starting process with command `node web.js`
2013-09-29T13:21:41.345806+00:00 app[web.1]: Listening on 20977
2013-09-29T13:21:41.368323+00:00 app[web.1]: { [error: relation "your_table" does not exist]
2013-09-29T13:21:41.368323+00:00 app[web.1]:   length: 101,
2013-09-29T13:21:41.368323+00:00 app[web.1]:   detail: undefined,
2013-09-29T13:21:41.368323+00:00 app[web.1]:   severity: 'ERROR',
2013-09-29T13:21:41.368323+00:00 app[web.1]:   hint: undefined,
2013-09-29T13:21:41.368323+00:00 app[web.1]:   position: '15',
2013-09-29T13:21:41.368323+00:00 app[web.1]:   code: '42P01',
2013-09-29T13:21:41.368323+00:00 app[web.1]:   name: 'error',
2013-09-29T13:21:41.368323+00:00 app[web.1]:   internalPosition: undefined,
2013-09-29T13:21:41.368512+00:00 app[web.1]:   where: undefined,
2013-09-29T13:21:41.368512+00:00 app[web.1]:   file: 'parse_relation.c',
2013-09-29T13:21:41.368512+00:00 app[web.1]:   line: '864',
2013-09-29T13:21:41.368323+00:00 app[web.1]:   internalQuery: undefined,
2013-09-29T13:21:41.368512+00:00 app[web.1]:   routine: 'parserOpenTable' }
2013-09-29T13:21:41.938926+00:00 heroku[web.1]: State changed from starting to up
2013-09-29T13:21:38.600520+00:00 heroku[api]: Deploy 95a0a35 by *********@gmail.com
2013-09-29T13:21:38.625733+00:00 heroku[api]: Release v17 created by *******@gmail.com
2013-09-29T13:22:08.383050+00:00 heroku[router]: at=info method=GET path=/ host=pure-lake-7106.herokuapp.com fwd="58.7.243.156" dyno=web.1 connect=3ms service=6ms status=200 bytes=12
2013-09-29T13:22:08.383327+00:00 app[web.1]: - - - [Sun, 29 Sep 2013 13:22:08 GMT] "GET / HTTP/1.1" 200 12 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/28.0.1500.71 Chrome/28.0.1500.71 Safari/537.36"
2013-09-29T13:22:10.046808+00:00 app[web.1]: - - - [Sun, 29 Sep 2013 13:22:10 GMT] "GET /favicon.ico HTTP/1.1" 404 - "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/28.0.1500.71 Chrome/28.0.1500.71 Safari/537.36"
2013-09-29T13:22:10.049179+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=pure-lake-7106.herokuapp.com fwd="58.7.243.156" dyno=web.1 connect=1ms service=3ms status=404 bytes=34
2013-09-29T13:29:40+00:00 heroku[slug-compiler]: Slug compilation started
2013-09-29T13:30:07.484077+00:00 heroku[api]: Deploy a2cc795 by xxxxxxxxxxx@gmail.com
2013-09-29T13:30:07.515481+00:00 heroku[api]: Release v18 created by xxxxxxxxx@gmail.com
2013-09-29T13:30:07+00:00 heroku[slug-compiler]: Slug compilation finished
2013-09-29T13:30:08.016355+00:00 heroku[web.1]: State changed from up to starting
2013-09-29T13:30:10.017792+00:00 heroku[web.1]: Starting process with command `node web.js`
2013-09-29T13:30:10.099473+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2013-09-29T13:30:11.008770+00:00 app[web.1]: Listening on 47344
2013-09-29T13:30:11.065531+00:00 app[web.1]:   name: 'error',
2013-09-29T13:30:11.065531+00:00 app[web.1]:   length: 101,
2013-09-29T13:30:11.065531+00:00 app[web.1]:   severity: 'ERROR',
2013-09-29T13:30:11.065531+00:00 app[web.1]:   code: '42P01',
2013-09-29T13:30:11.065531+00:00 app[web.1]:   detail: undefined,
2013-09-29T13:30:11.065531+00:00 app[web.1]:   position: '15',
2013-09-29T13:30:11.065531+00:00 app[web.1]: { [error: relation "your_table" does not exist]
2013-09-29T13:30:11.065531+00:00 app[web.1]:   internalPosition: undefined,
2013-09-29T13:30:11.065531+00:00 app[web.1]:   internalQuery: undefined,
2013-09-29T13:30:11.065840+00:00 app[web.1]:   where: undefined,
2013-09-29T13:30:11.065840+00:00 app[web.1]:   file: 'parse_relation.c',
2013-09-29T13:30:11.065840+00:00 app[web.1]:   line: '864',
2013-09-29T13:30:11.065840+00:00 app[web.1]:   routine: 'parserOpenTable' }
2013-09-29T13:30:11.065531+00:00 app[web.1]:   hint: undefined,
2013-09-29T13:30:11.482704+00:00 heroku[web.1]: State changed from starting to up
2013-09-29T13:30:11.651117+00:00 heroku[web.1]: Process exited with status 143
2013-09-29T13:30:17.729604+00:00 app[web.1]: - - - [Sun, 29 Sep 2013 13:30:17 GMT] "GET / HTTP/1.1" 200 12 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/28.0.1500.71 Chrome/28.0.1500.71 Safari/537.36"
2013-09-29T13:30:19.361615+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=pure-lake-7106.herokuapp.com fwd="58.7.243.156" dyno=web.1 connect=1ms service=3ms status=404 bytes=34
2013-09-29T13:30:19.364457+00:00 app[web.1]: - - - [Sun, 29 Sep 2013 13:30:19 GMT] "GET /favicon.ico HTTP/1.1" 404 - "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/28.0.1500.71 Chrome/28.0.1500.71 Safari/537.36"
2013-09-29T13:30:17.728815+00:00 heroku[router]: at=info method=GET path=/ host=pure-lake-7106.herokuapp.com fwd="58.7.243.156" dyno=web.1 connect=1ms service=19ms status=200 bytes=12
2013-09-29T13:32:28+00:00 heroku[slug-compiler]: Slug compilation started
2013-09-29T13:32:43.338858+00:00 heroku[api]: Deploy d4cf2ba by xxxxxxxx@gmail.com
2013-09-29T13:32:43.359317+00:00 heroku[api]: Release v19 created by xxxxxxxx@gmail.com
2013-09-29T13:32:43+00:00 heroku[slug-compiler]: Slug compilation finished
2013-09-29T13:32:43.746015+00:00 heroku[web.1]: State changed from up to starting
2013-09-29T13:32:45.354842+00:00 heroku[web.1]: Starting process with command `node web.js`
2013-09-29T13:32:46.098651+00:00 app[web.1]: Listening on 37156
2013-09-29T13:32:47.127328+00:00 app[web.1]: { [error: relation "your_table" does not exist]
2013-09-29T13:32:47.127328+00:00 app[web.1]:   code: '42P01',
2013-09-29T13:32:47.127328+00:00 app[web.1]:   name: 'error',
2013-09-29T13:32:47.127328+00:00 app[web.1]:   length: 101,
2013-09-29T13:32:47.127328+00:00 app[web.1]:   hint: undefined,
2013-09-29T13:32:47.127328+00:00 app[web.1]:   position: '15',
2013-09-29T13:32:47.127328+00:00 app[web.1]:   severity: 'ERROR',
2013-09-29T13:32:47.127328+00:00 app[web.1]:   detail: undefined,
2013-09-29T13:32:47.127561+00:00 app[web.1]:   where: undefined,
2013-09-29T13:32:47.127561+00:00 app[web.1]:   routine: 'parserOpenTable' }
2013-09-29T13:32:47.127328+00:00 app[web.1]:   internalPosition: undefined,
2013-09-29T13:32:47.127328+00:00 app[web.1]:   internalQuery: undefined,
2013-09-29T13:32:47.127561+00:00 app[web.1]:   file: 'parse_relation.c',
2013-09-29T13:32:47.127561+00:00 app[web.1]:   line: '864',
2013-09-29T13:32:47.197293+00:00 heroku[web.1]: State changed from starting to up
2013-09-29T13:32:50.505267+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2013-09-29T13:32:53.246120+00:00 heroku[web.1]: Process exited with status 143
2013-09-29T14:39:50.833246+00:00 heroku[web.1]: Idling
2013-09-29T14:39:52.828292+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2013-09-29T14:39:54.545662+00:00 heroku[web.1]: Process exited with status 143
2013-09-29T14:39:54.559151+00:00 heroku[web.1]: State changed from up to down

I get this erroe when I do a foreman start

p$ foreman start
12:39:41 web.1  | started with pid 13983
12:39:41 web.1  | Listening on 5000
12:39:41 web.1  | /home/roland/github/heroku_app/web.js:18
12:39:41 web.1  |   client.query('SELECT * FROM your_table', function(err, result) {
12:39:41 web.1  |          ^
12:39:41 web.1  | TypeError: Cannot call method 'query' of null
12:39:41 web.1  |     at /home/roland/github/heroku_app/web.js:18:10
12:39:41 web.1  |     at /home/roland/github/heroku_app/node_modules/pg/lib/pool.js:54:25
12:39:41 web.1  |     at /home/roland/github/heroku_app/node_modules/pg/node_modules/generic-pool/lib/generic-pool.js:271:11
12:39:41 web.1  |     at /home/roland/github/heroku_app/node_modules/pg/lib/pool.js:27:26
12:39:41 web.1  |     at null.<anonymous> (/home/roland/github/heroku_app/node_modules/pg/lib/client.js:169:9)
12:39:41 web.1  |     at EventEmitter.emit (events.js:95:17)
12:39:41 web.1  |     at null.<anonymous> (/home/roland/github/heroku_app/node_modules/pg/lib/connection.js:97:12)
12:39:41 web.1  |     at Socket.EventEmitter.emit (events.js:95:17)
12:39:41 web.1  |     at Socket.<anonymous> (_stream_readable.js:746:14)
12:39:41 web.1  |     at Socket.EventEmitter.emit (events.js:92:17)
12:39:41 web.1  | exited with code 8
12:39:41 system | sending SIGTERM to all processes
SIGTERM received

EDIT my console.logs;

var express = require("express");
var app = express();
app.use(express.logger());

app.get('/', function(request, response) {
  response.send('Hello World!');
  console.log("hello roland");
});

var port = process.env.PORT || 5000;
app.listen(port, function() {
  console.log("Listening on " + port);
});

var pg = require('pg');


pg.connect(process.env.DATABASE_URL, function(err, client, done) {
   console.log(err+"!!!!!!!!!!!!!!!");
  client.query('SELECT * FROM your_table', function(err, result) {
    done();
    if(err) return console.error(err);
    console.log(result.rows);
  });
});

解决方案

It's Heroku's problem. The "process.env.DATABASE_URL" variable they tell you to use in pg.connect is not functioning.

A simple

console.log(process.env.DATABASE_URL);

Will show that this variable is undefined.

Until Heroku offers a fix, you can hard-code the connection URL as the first argument to pg.connect().

To find your credentials, you can go to your app's PostgreSQL add-on connection settings through http://heroku.com.

The new pg.connect method will look like

var connectionString = "postgres://*USERNAME*:*PASSWORD*@*HOST*:*PORT*/*DATABASE*"

pg.connect(connectionString, function(err, client, done) {
   client.query('SELECT * FROM your_table', function(err, result) {
      done();
      if(err) return console.error(err);
      console.log(result.rows);
   });
});

这篇关于尝试将我的node.js连接到Heroku PostgreSQL数据库。 Heroku Postgres教程之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆