I'm attempting to display rows of data from a private space Postgres instance attached to app-a from another app - app-b, inside the same Private Space.
Running heroku local didn't work - which I thought was due to my personal IP not being whitelisted, so I pushed to app-b. I've also added the credential for app-b - to no avail. app-b has no DB of it's own and I've tried adding credential while DATABASE_URL is manually filled, and with no DATABASE_URL on abb-b - none of which worked.
I'm able to run heroku pg:psql from app-a but not from app-b, leading me to think the credential may not be attached/incorrectly attached?
My code is simple and shown below:
var { Client } = require('pg');
app.get('/db', (req, res) => {
var dbOpts = {
connectionString: process.env.DATABASE_URL,
ssl : {
rejectUnauthorized: false
}
}
const client = new Client(dbOpts);
client.connect();
client.query('SELECT FirstName, Id FROM salesforce.user', (err, dbRes) => {
console.log('inside');
if (err) {
console.log('DB Route err: ', err);
throw err;
}
console.log('dbRes: ', dbRes);
res.render('pages/db',{
results : dbRes.rows
});
});
client.end();
});
As soon as I hit /db the following error is thrown
2022-03-04T08:21:30.057536+00:00 app[web.1]: /app/index.js:25
2022-03-04T08:21:30.057537+00:00 app[web.1]: throw err;
2022-03-04T08:21:30.057537+00:00 app[web.1]: ^
2022-03-04T08:21:30.057537+00:00 app[web.1]:
2022-03-04T08:21:30.057537+00:00 app[web.1]: Error: Connection terminated
2022-03-04T08:21:30.057538+00:00 app[web.1]: at Connection.<anonymous> (/app/node_modules/pg/lib/client.js:132:36)
2022-03-04T08:21:30.057538+00:00 app[web.1]: at Object.onceWrapper (node:events:641:28)
2022-03-04T08:21:30.057538+00:00 app[web.1]: at Connection.emit (node:events:539:35)
2022-03-04T08:21:30.057539+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/pg/lib/connection.js:57:12)
2022-03-04T08:21:30.057539+00:00 app[web.1]: at Socket.emit (node:events:527:28)
2022-03-04T08:21:30.057539+00:00 app[web.1]: at TCP.<anonymous> (node:net:688:12)
2022-03-04T08:21:30.057540+00:00 app[web.1]:
2022-03-04T08:21:30.057540+00:00 app[web.1]: Node.js v17.6.0
2022-03-04T08:21:30.055860+00:00 app[web.1]: inside
2022-03-04T08:21:30.057190+00:00 app[web.1]: DB Route err: Error: Connection terminated
2022-03-04T08:21:30.057191+00:00 app[web.1]: at Connection.<anonymous> (/app/node_modules/pg/lib/client.js:132:36)
2022-03-04T08:21:30.057192+00:00 app[web.1]: at Object.onceWrapper (node:events:641:28)
2022-03-04T08:21:30.057192+00:00 app[web.1]: at Connection.emit (node:events:539:35)
2022-03-04T08:21:30.057192+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/pg/lib/connection.js:57:12)
2022-03-04T08:21:30.057193+00:00 app[web.1]: at Socket.emit (node:events:527:28)
2022-03-04T08:21:30.057193+00:00 app[web.1]: at TCP.<anonymous> (node:net:688:12)
2022-03-04T08:21:30.060340+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/db" host=agdc-hconnect-view.herokuapp.com request_id=3e850bf1-c88a-68bc-6f21-0ef82b960ce9 fwd="75.111.73.234" dyno=web.1 connect=0ms service=19ms status=503 bytes=561 protocol=http tls_version=tls1.3
2022-03-04T08:21:30.659072+00:00 heroku[web.1]: Process exited with status 1
2022-03-04T08:21:30.679550+00:00 heroku[web.1]: State changed from up to crashed
Related
I have a tiny backend with literally 1 endpoint. It works flawlessly on my local machine. After a cold startup, it takes roughly 4 seconds to return on my local machine. But when I deploy to Heroku, it boots up and loads fine, but if you try to utilize it, it crashes with code H13 which after some research is equivalent to Heroku intentionally crashing it because it took too long to load. From what I can tell it's supposed to happen after 30 seconds of attempting to load, though after timing it, it's closer to about 10 before it crashes. I've also seen 1 potential workaround where they used a timeout function every 3 seconds to check the status of the server, though frankly the code was beyond me. My best guess is that the await OpenseaScraper is taking too long to respond, but a single call to an api shouldn't be that long right?
Here is my code
const express = require('express');
const OpenseaScraper = require('opensea-scraper')
const mongoose = require('mongoose');
require('dotenv').config()
const NFT = require('./NFTModel')
const server = express()
const router = express.Router()
mongoose.connect(process.env.MONGODB_URL, () => console.log('This application has been connected to the Mogo DB!'))
router.get('/', async (req, res) => {
const ranking = await OpenseaScraper.rankings()
let NFTs = []
try {
ranking.forEach(async item => {
const slug = item.slug
const name = item.name
const logoUrl = item.logo
let floorPrice = 0
if (item.floorPrice == null) {
floorPrice = 0
} else {
floorPrice = item.floorPrice.amount
}
NFTs.push({name, slug, logoUrl, floorPrice})
NFT.findOne({ "slug": slug }, (err, value) => {
if (err) { console.error(err) }
if (!value) {
let newNFT = new NFT({slug, name, logoUrl, floorPrice})
newNFT.save() }
})
})
} catch(err) {
console.log(err)
res.status(400).end()
}
res.json(NFTs)
})
server.use(router)
server.listen(process.env.PORT || 5000, () => {
console.log(`Express is working on port ${process.env.PORT}`);
});
Here is the [crash]
[1]: https://i.stack.imgur.com/8GvD4.png
2022-06-12T02:52:42.150870+00:00 heroku[web.1]: State changed from starting to up
2022-06-12T02:52:42.485695+00:00 app[web.1]: This application has been connected to the Mogo DB!
2022-06-12T02:53:19.480795+00:00 app[web.1]: /app/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:241
2022-06-12T02:53:19.480803+00:00 app[web.1]: reject(new Error([
2022-06-12T02:53:19.480803+00:00 app[web.1]: ^
2022-06-12T02:53:19.480803+00:00 app[web.1]:
2022-06-12T02:53:19.480804+00:00 app[web.1]: Error: Failed to launch the browser process!
2022-06-12T02:53:19.480818+00:00 app[web.1]: /app/node_modules/puppeteer/.local-chromium/linux-982053/chrome-linux/chrome: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory
2022-06-12T02:53:19.480818+00:00 app[web.1]:
2022-06-12T02:53:19.480818+00:00 app[web.1]:
2022-06-12T02:53:19.480819+00:00 app[web.1]: TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md
2022-06-12T02:53:19.480819+00:00 app[web.1]:
2022-06-12T02:53:19.480819+00:00 app[web.1]: at onClose (/app/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:241:20)
2022-06-12T02:53:19.480820+00:00 app[web.1]: at Interface.<anonymous> (/app/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:231:68)
2022-06-12T02:53:19.480820+00:00 app[web.1]: at Interface.emit (node:events:539:35)
2022-06-12T02:53:19.480820+00:00 app[web.1]: at Interface.close (node:readline:586:8)
2022-06-12T02:53:19.480821+00:00 app[web.1]: at Socket.onend (node:readline:277:10)
2022-06-12T02:53:19.480822+00:00 app[web.1]: at Socket.emit (node:events:539:35)
2022-06-12T02:53:19.480822+00:00 app[web.1]: at endReadableNT (node:internal/streams/readable:1345:12)
2022-06-12T02:53:19.480822+00:00 app[web.1]: at processTicksAndRejections (node:internal/process/task_queues:83:21)
2022-06-12T02:53:19.485994+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/" host=nft-backend-open-sea.herokuapp.com request_id=22e86188-d5ab-4e1a-a123-64ade9c345ab fwd="216.198.98.78" dyno=web.1 connect=0ms service=2510ms status=503 bytes=0 protocol=https
2022-06-12T02:53:19.594772+00:00 heroku[web.1]: Process exited with status 1
2022-06-12T02:53:19.717227+00:00 heroku[web.1]: State changed from up to crashed
Please help. I keep getting this H10 error when I deploy my PERN project on Heroku. I suspect the error may be coming from postgres as the logs seem to say but I am out of ideas. I have set the port to "process.env.PORT" as most suggestions say and config variables are on Heroku .
here is my server.js
const http = require('http');
const app = require('./app');
const db = require('./db/db.js')
db.authenticate()
.then(()=>{console.log('connected successfully')
main()
})
.catch(error =>(console.log(error)))
const main = async () => {
const normalizePort = val => {
const port = parseInt(val, 10);
if (isNaN(port)) {
return val;
}
if (port >= 0) {
return port;
}
return false;
};
const port = normalizePort(process.env.PORT || '8000');
app.set('port', port);
const errorHandler = error => {
if (error.syscall !== 'listen') {
throw error;
}
const address = server.address();
const bind = typeof address === 'string' ? 'pipe ' + address : 'port: ' + port;
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges.');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use.');
process.exit(1);
break;
default:
throw error;
}
};
const server = http.createServer(app);
server.on('error', errorHandler);
server.on('listening', () => {
const address = server.address();
const bind = typeof address === 'string' ? 'pipe ' + address : 'port ' + port;
console.log('Listening on ' + bind);
});
app.use(express.static(path.join(__dirname, "/client/build")));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '/client/build', 'index.html'));
});
server.listen(port);
}
and my app.js
```
const express = require('express');
const app = express();
const postRoutes = require('./routes/post');
const userRoutes = require('./routes/user');
const path = require('path');
const bodyparser = require('body-parser')
```
//app.use(express.json());
app.use(bodyparser.json())
app.use(bodyparser.urlencoded({extended:true}))
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content, Accept, Content-Type, Authorization');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
next();
});
app.use('/images', express.static(path.join(__dirname, 'images')));
app.use('/api/posts', postRoutes);
app.use('/api/auth', userRoutes);
app.use((req, res, next) => {
if(req.url.includes("/images/")){
res.sendFile(path.join(__dirname, "images/picture.png"))
}
console.log(req.url)
next()
})
module.exports = app;
my heroku logs:
» Warning: heroku update available from 7.53.0 to 7.60.1.
2022-04-08T05:53:25.248770+00:00 app[web.1]: triggerUncaughtException(err, true /* fromPromise */);
2022-04-08T05:53:25.248770+00:00 app[web.1]: ^
2022-04-08T05:53:25.248770+00:00 app[web.1]:
2022-04-08T05:53:25.248771+00:00 app[web.1]: ConnectionRefusedError [SequelizeConnectionRefusedError]: connect ECONNREFUSED 127.0.0.1:5432
2022-04-08T05:53:25.248772+00:00 app[web.1]: at Client._connectionCallback (/app/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:130:24)
2022-04-08T05:53:25.248772+00:00 app[web.1]: at Client._handleErrorWhileConnecting (/app/node_modules/pg/lib/client.js:305:19)
2022-04-08T05:53:25.248773+00:00 app[web.1]: at Client._handleErrorEvent (/app/node_modules/pg/lib/client.js:315:19)
2022-04-08T05:53:25.248773+00:00 app[web.1]: at Connection.emit (node:events:526:28)
2022-04-08T05:53:25.248773+00:00 app[web.1]: at Socket.reportStreamError (/app/node_modules/pg/lib/connection.js:52:12)
2022-04-08T05:53:25.248774+00:00 app[web.1]: at Socket.emit (node:events:526:28)
2022-04-08T05:53:25.248774+00:00 app[web.1]: at emitErrorNT (node:internal/streams/destroy:157:8)
2022-04-08T05:53:25.248774+00:00 app[web.1]: at emitErrorCloseNT (node:internal/streams/destroy:122:3)
2022-04-08T05:53:25.248775+00:00 app[web.1]: at processTicksAndRejections (node:internal/process/task_queues:83:21) {
2022-04-08T05:53:25.248775+00:00 app[web.1]: parent: Error: connect ECONNREFUSED 127.0.0.1:5432
2022-04-08T05:53:25.248775+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1157:16) {
2022-04-08T05:53:25.248775+00:00 app[web.1]: errno: -111,
2022-04-08T05:53:25.248776+00:00 app[web.1]: code: 'ECONNREFUSED',
2022-04-08T05:53:25.248776+00:00 app[web.1]: syscall: 'connect',
2022-04-08T05:53:25.248776+00:00 app[web.1]: address: '127.0.0.1',
2022-04-08T05:53:25.248776+00:00 app[web.1]: port: 5432
2022-04-08T05:53:25.248776+00:00 app[web.1]: },
2022-04-08T05:53:25.248777+00:00 app[web.1]: original: Error: connect ECONNREFUSED 127.0.0.1:5432
2022-04-08T05:53:25.248777+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1157:16) {
2022-04-08T05:53:25.248777+00:00 app[web.1]: errno: -111,
2022-04-08T05:53:25.248777+00:00 app[web.1]: code: 'ECONNREFUSED',
2022-04-08T05:53:25.248778+00:00 app[web.1]: syscall: 'connect',
2022-04-08T05:53:25.248778+00:00 app[web.1]: address: '127.0.0.1',
2022-04-08T05:53:25.248778+00:00 app[web.1]: port: 5432
2022-04-08T05:53:25.248778+00:00 app[web.1]: }
2022-04-08T05:53:25.248779+00:00 app[web.1]: }
2022-04-08T05:53:25.473441+00:00 heroku[web.1]: Process exited with status 1
2022-04-08T05:53:25.545533+00:00 heroku[web.1]: State changed from starting to crashed
2022-04-08T11:21:39.958520+00:00 heroku[web.1]: State changed from crashed to starting
2022-04-08T11:21:49.686507+00:00 heroku[web.1]: Starting process with command `npm start`
2022-04-08T11:21:50.907291+00:00 app[web.1]:
2022-04-08T11:21:50.907304+00:00 app[web.1]: > backend#1.0.0 start
2022-04-08T11:21:50.907304+00:00 app[web.1]: > node server.js
2022-04-08T11:21:50.907304+00:00 app[web.1]:
2022-04-08T11:21:51.291113+00:00 app[web.1]: ConnectionRefusedError [SequelizeConnectionRefusedError]: connect ECONNREFUSED 127.0.0.1:5432
2022-04-08T11:21:51.291123+00:00 app[web.1]: at Client._connectionCallback (/app/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:130:24)
2022-04-08T11:21:51.291123+00:00 app[web.1]: at Client._handleErrorWhileConnecting (/app/node_modules/pg/lib/client.js:305:19)
2022-04-08T11:21:51.291124+00:00 app[web.1]: at Client._handleErrorEvent (/app/node_modules/pg/lib/client.js:315:19)
2022-04-08T11:21:51.291125+00:00 app[web.1]: at Connection.emit (node:events:526:28)
2022-04-08T11:21:51.291126+00:00 app[web.1]: at Socket.reportStreamError (/app/node_modules/pg/lib/connection.js:52:12)
2022-04-08T11:21:51.291126+00:00 app[web.1]: at Socket.emit (node:events:526:28)
2022-04-08T11:21:51.291127+00:00 app[web.1]: at emitErrorNT (node:internal/streams/destroy:157:8)
2022-04-08T11:21:51.291127+00:00 app[web.1]: at emitErrorCloseNT (node:internal/streams/destroy:122:3)
2022-04-08T11:21:51.291128+00:00 app[web.1]: at processTicksAndRejections (node:internal/process/task_queues:83:21) {
2022-04-08T11:21:51.291128+00:00 app[web.1]: parent: Error: connect ECONNREFUSED 127.0.0.1:5432
2022-04-08T11:21:51.291128+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1157:16) {
2022-04-08T11:21:51.291129+00:00 app[web.1]: errno: -111,
2022-04-08T11:21:51.291129+00:00 app[web.1]: code: 'ECONNREFUSED',
2022-04-08T11:21:51.291129+00:00 app[web.1]: syscall: 'connect',
2022-04-08T11:21:51.291130+00:00 app[web.1]: address: '127.0.0.1',
2022-04-08T11:21:51.291131+00:00 app[web.1]: port: 5432
2022-04-08T11:21:51.291131+00:00 app[web.1]: },
2022-04-08T11:21:51.291131+00:00 app[web.1]: original: Error: connect ECONNREFUSED 127.0.0.1:5432
2022-04-08T11:21:51.291132+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1157:16) {
2022-04-08T11:21:51.291132+00:00 app[web.1]: errno: -111,
2022-04-08T11:21:51.291132+00:00 app[web.1]: code: 'ECONNREFUSED',
2022-04-08T11:21:51.291132+00:00 app[web.1]: syscall: 'connect',
2022-04-08T11:21:51.291133+00:00 app[web.1]: address: '127.0.0.1',
2022-04-08T11:21:51.291133+00:00 app[web.1]: port: 5432
2022-04-08T11:21:51.291133+00:00 app[web.1]: }
2022-04-08T11:21:51.291134+00:00 app[web.1]: }
2022-04-08T11:21:51.291954+00:00 app[web.1]: node:internal/process/promises:279
2022-04-08T11:21:51.291956+00:00 app[web.1]: triggerUncaughtException(err, true /* fromPromise */);
2022-04-08T11:21:51.291957+00:00 app[web.1]: ^
2022-04-08T11:21:51.291957+00:00 app[web.1]:
2022-04-08T11:21:51.291957+00:00 app[web.1]: ConnectionRefusedError [SequelizeConnectionRefusedError]: connect ECONNREFUSED 127.0.0.1:5432
2022-04-08T11:21:51.291958+00:00 app[web.1]: at Client._connectionCallback (/app/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:130:24)
2022-04-08T11:21:51.291958+00:00 app[web.1]: at Client._handleErrorWhileConnecting (/app/node_modules/pg/lib/client.js:305:19)
2022-04-08T11:21:51.291959+00:00 app[web.1]: at Client._handleErrorEvent (/app/node_modules/pg/lib/client.js:315:19)
2022-04-08T11:21:51.291959+00:00 app[web.1]: at Connection.emit (node:events:526:28)
2022-04-08T11:21:51.291959+00:00 app[web.1]: at Socket.reportStreamError (/app/node_modules/pg/lib/connection.js:52:12)
2022-04-08T11:21:51.291959+00:00 app[web.1]: at Socket.emit (node:events:526:28)
2022-04-08T11:21:51.291960+00:00 app[web.1]: at emitErrorNT (node:internal/streams/destroy:157:8)
2022-04-08T11:21:51.291960+00:00 app[web.1]: at emitErrorCloseNT (node:internal/streams/destroy:122:3)
2022-04-08T11:21:51.291960+00:00 app[web.1]: at processTicksAndRejections (node:internal/process/task_queues:83:21) {
2022-04-08T11:21:51.291961+00:00 app[web.1]: parent: Error: connect ECONNREFUSED 127.0.0.1:5432
2022-04-08T11:21:51.291961+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1157:16) {
2022-04-08T11:21:51.291961+00:00 app[web.1]: errno: -111,
2022-04-08T11:21:51.291962+00:00 app[web.1]: code: 'ECONNREFUSED',
2022-04-08T11:21:51.291962+00:00 app[web.1]: syscall: 'connect',
2022-04-08T11:21:51.291962+00:00 app[web.1]: address: '127.0.0.1',
2022-04-08T11:21:51.291962+00:00 app[web.1]: port: 5432
2022-04-08T11:21:51.291963+00:00 app[web.1]: },
2022-04-08T11:21:51.291963+00:00 app[web.1]: original: Error: connect ECONNREFUSED 127.0.0.1:5432
2022-04-08T11:21:51.291963+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1157:16) {
2022-04-08T11:21:51.291963+00:00 app[web.1]: errno: -111,
2022-04-08T11:21:51.291964+00:00 app[web.1]: code: 'ECONNREFUSED',
2022-04-08T11:21:51.291964+00:00 app[web.1]: syscall: 'connect',
2022-04-08T11:21:51.291964+00:00 app[web.1]: address: '127.0.0.1',
2022-04-08T11:21:51.291964+00:00 app[web.1]: port: 5432
2022-04-08T11:21:51.291964+00:00 app[web.1]: }
2022-04-08T11:21:51.291965+00:00 app[web.1]: }
2022-04-08T11:21:51.453104+00:00 heroku[web.1]: Process exited with status 1
2022-04-08T11:21:51.661389+00:00 heroku[web.1]: State changed from starting to crashed
2022-04-08T13:41:28.066268+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=socialapp400.herokuapp.com request_id=6e193689-d520-4a3b-b74f-3f57fbb98240 fwd="41.90.63.133" dyno= connect= service= status=503 bytes= protocol=https
2022-04-08T13:41:37.732157+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/api" host=socialapp400.herokuapp.com request_id=dae9e423-59d3-4124-b8fa-d8983e1ecfbf fwd="41.90.63.133" dyno= connect= service= status=503 bytes= protocol=https
2022-04-08T14:11:40.368593+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=socialapp400.herokuapp.com request_id=2eae5ca8-4b92-46f0-b223-d6e973b4f249 fwd="41.90.63.133" dyno= connect= service= status=503 bytes= protocol=https
I am in the process of deploying my app in heroku however I have ran into some difficulties. I am able to connect to my mlab mongo database on my local computer and using the heroku local web (although for reasons I have not looked into yet I cannot query the database). Below is my error from the heroku logs:
017-06-30T03:18:42.576989+00:00 app[web.1]: /app/node_modules/mongodb/lib/mongo_client.js:377
2017-06-30T03:18:42.576990+00:00 app[web.1]: throw err
2017-06-30T03:18:42.576991+00:00 app[web.1]: ^
2017-06-30T03:18:42.576996+00:00 app[web.1]: MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
2017-06-30T03:18:42.576997+00:00 app[web.1]: at Pool.<anonymous> (/app/node_modules/mongodb-core/lib/topologies/server.js:328:35)
2017-06-30T03:18:42.576998+00:00 app[web.1]: at emitOne (events.js:115:13)
2017-06-30T03:18:42.576999+00:00 app[web.1]: at Pool.emit (events.js:210:7)
2017-06-30T03:18:42.576999+00:00 app[web.1]: at Connection.<anonymous> (/app/node_modules/mongodb-core/lib/connection/pool.js:280:12)
2017-06-30T03:18:42.577000+00:00 app[web.1]: at Object.onceWrapper (events.js:318:30)
2017-06-30T03:18:42.577001+00:00 app[web.1]: at emitTwo (events.js:125:13)
2017-06-30T03:18:42.577001+00:00 app[web.1]: at Connection.emit (events.js:213:7)
2017-06-30T03:18:42.577002+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mongodb-core/lib/connection/connection.js:177:49)
2017-06-30T03:18:42.577003+00:00 app[web.1]: at Object.onceWrapper (events.js:316:30)
2017-06-30T03:18:42.577004+00:00 app[web.1]: at emitOne (events.js:115:13)
2017-06-30T03:18:42.577004+00:00 app[web.1]: at Socket.emit (events.js:210:7)
2017-06-30T03:18:42.577005+00:00 app[web.1]: at emitErrorNT (internal/streams/destroy.js:62:8)
2017-06-30T03:18:42.577006+00:00 app[web.1]: at _combinedTickCallback (internal/process/next_tick.js:102:11)
2017-06-30T03:18:42.577007+00:00 app[web.1]: at process._tickCallback (internal/process/next_tick.js:161:9)
2017-06-30T03:18:42.677447+00:00 heroku[web.1]: State changed from starting to crashed
2017-06-30T03:18:42.671887+00:00 heroku[web.1]: Process exited with status 1
My code is as follows (I have removed some stuff just to try and keep it brief):
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
var mongoose = require('mongoose');
var mongoStore = require('connect-mongo/es5')({ session: expressSession });
require('./models/users_model.js');
mongoose.Promise = global.Promise;
var MONGOLAB_URI = "mongodb://username:password# etc.";
var connection = mongoose.connect(MONGOLAB_URI, {useMongoClient: true},function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
console.log('Connection established to', MONGOLAB_URI);
}
});
My app works locally. I added all that I thought was necessary for Heroku
Procfile
web: node app.js
package.json
{
"name": "HipsterMatch",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "nodemon app.js"
},
"dependencies": {
"express": "3.1.0",
"jade": "*",
"mongodb": "*",
"nodemon": "0.7.10",
"passport": "*",
"passport-local": "*",
"passport-facebook": "*",
"mongoose": "~3.6.4",
"connect-flash": "*"
},
"engines": {
"node": "0.10.1",
"npm": "1.3.14"
}
}
Here are my logs
2013-12-27T08:40:29.457548+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/ host=hipsterlove.herokuapp.com fwd="162.205.69.18" dyno= connect= service= status=503 bytes=
2013-12-27T08:44:38.166741+00:00 heroku[api]: Add mongohq:sandbox add-on by jgallardo720#gmail.com
2013-12-27T08:44:38.186021+00:00 heroku[api]: Release v5 created by jgallardo720#gmail.com
2013-12-27T08:44:38.327041+00:00 heroku[web.1]: State changed from crashed to starting
2013-12-27T08:44:40.396197+00:00 heroku[web.1]: Starting process with command `node app.js`
2013-12-27T08:44:42.090303+00:00 app[web.1]: Express server listening on port 37441
2013-12-27T08:44:42.092470+00:00 app[web.1]:
2013-12-27T08:44:42.092672+00:00 app[web.1]: events.js:72
2013-12-27T08:44:42.092911+00:00 app[web.1]: throw er; // Unhandled 'error' event
2013-12-27T08:44:42.092911+00:00 app[web.1]: ^
2013-12-27T08:44:42.095509+00:00 app[web.1]: at null.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:540:74)
2013-12-27T08:44:42.095509+00:00 app[web.1]: Error: failed to connect to [localhost:27017]
2013-12-27T08:44:42.095509+00:00 app[web.1]: at EventEmitter.emit (events.js:106:17)
2013-12-27T08:44:42.095509+00:00 app[web.1]: at null.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:140:15)
2013-12-27T08:44:42.095509+00:00 app[web.1]: at process._tickCallback (node.js:415:13)
2013-12-27T08:44:42.095509+00:00 app[web.1]: at EventEmitter.emit (events.js:98:17)
2013-12-27T08:44:42.095509+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:478:10)
2013-12-27T08:44:42.095509+00:00 app[web.1]: at Socket.EventEmitter.emit (events.js:95:17)
2013-12-27T08:44:42.095509+00:00 app[web.1]: at net.js:426:14
2013-12-27T08:44:43.403162+00:00 heroku[web.1]: Process exited with status 8
2013-12-27T08:44:43.421294+00:00 heroku[web.1]: State changed from starting to crashed
2013-12-27T08:45:03.209570+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/ host=hipsterlove.herokuapp.com fwd="162.205.69.18" dyno= connect= service= status=503 bytes=
2013-12-27T08:47:32.961418+00:00 heroku[api]: Add papertrail:choklad add-on by jgallardo720#gmail.com
2013-12-27T08:47:33.079955+00:00 heroku[api]: Release v6 created by jgallardo720#gmail.com
2013-12-27T08:47:33.710788+00:00 heroku[web.1]: State changed from crashed to starting
2013-12-27T08:47:36.186033+00:00 heroku[web.1]: Starting process with command `node app.js`
2013-12-27T08:47:37.958627+00:00 app[web.1]: at null.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:540:74)
2013-12-27T08:47:37.958627+00:00 app[web.1]: Error: failed to connect to [localhost:27017]
2013-12-27T08:47:37.958627+00:00 app[web.1]: at EventEmitter.emit (events.js:106:17)
2013-12-27T08:47:37.954398+00:00 app[web.1]: ^
2013-12-27T08:47:37.949510+00:00 app[web.1]: Express server listening on port 32125
2013-12-27T08:47:37.952974+00:00 app[web.1]:
2013-12-27T08:47:37.953554+00:00 app[web.1]: events.js:72
2013-12-27T08:47:37.954045+00:00 app[web.1]: throw er; // Unhandled 'error' event
2013-12-27T08:47:37.958627+00:00 app[web.1]: at null.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:140:15)
2013-12-27T08:47:37.958627+00:00 app[web.1]: at EventEmitter.emit (events.js:98:17)
2013-12-27T08:47:37.958627+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:478:10)
2013-12-27T08:47:37.958627+00:00 app[web.1]: at Socket.EventEmitter.emit (events.js:95:17)
2013-12-27T08:47:37.958627+00:00 app[web.1]: at net.js:426:14
2013-12-27T08:47:37.958627+00:00 app[web.1]: at process._tickCallback (node.js:415:13)
2013-12-27T08:47:39.133359+00:00 heroku[web.1]: State changed from starting to crashed
2013-12-27T08:47:39.134204+00:00 heroku[web.1]: State changed from crashed to starting
2013-12-27T08:47:39.120176+00:00 heroku[web.1]: Process exited with status 8
2013-12-27T08:47:41.463835+00:00 heroku[web.1]: Starting process with command `node app.js`
2013-12-27T08:47:42.782433+00:00 app[web.1]: Express server listening on port 7709
2013-12-27T08:47:42.784187+00:00 app[web.1]:
2013-12-27T08:47:42.784508+00:00 app[web.1]: events.js:72
2013-12-27T08:47:42.784983+00:00 app[web.1]: throw er; // Unhandled 'error' event
2013-12-27T08:47:42.784983+00:00 app[web.1]: ^
2013
-12-27T08:47:42.787164+00:00 app[web.1]: Error: failed to connect to [localhost:27017]
2013-12-27T08:47:42.787164+00:00 app[web.1]: at null.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:540:74)
2013-12-27T08:47:42.787164+00:00 app[web.1]: at EventEmitter.emit (events.js:106:17)
2013-12-27T08:47:42.787164+00:00 app[web.1]: at null.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:140:15)
2013-12-27T08:47:42.787164+00:00 app[web.1]: at EventEmitter.emit (events.js:98:17)
2013-12-27T08:47:42.787164+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:478:10)
2013-12-27T08:47:42.787164+00:00 app[web.1]: at Socket.EventEmitter.emit (events.js:95:17)
2013-12-27T08:47:42.787164+00:00 app[web.1]: at net.js:426:14
2013-12-27T08:47:42.787164+00:00 app[web.1]: at process._tickCallback (node.js:415:13)
2013-12-27T08:47:44.091837+00:00 heroku[web.1]: Process exited with status 8
2013-12-27T08:47:44.093316+00:00 heroku[web.1]: State changed from starting to crashed
2013-12-27T08:50:30+00:00 heroku[slug-compiler]: Slug compilation started
2013-12-27T08:50:55.506146+00:00 heroku[api]: Deploy 36c405b by jgallardo720#gmail.com
2013-12-27T08:50:55.532599+00:00 heroku[api]: Release v7 created by jgallardo720#gmail.com
2013-12-27T08:50:55+00:00 heroku[slug-compiler]: Slug compilation finished
2013-12-27T08:50:55.924898+00:00 heroku[web.1]: State changed from crashed to starting
2013-12-27T08:50:57.601471+00:00 heroku[web.1]: Starting process with command `node app.js`
2013-12-27T08:50:58.821299+00:00 app[web.1]: Express server listening on port 44602
2013-12-27T08:50:58.823409+00:00 app[web.1]: events.js:72
2013-12-27T08:50:58.823687+00:00 app[web.1]: ^
2013-12-27T08:50:58.823080+00:00 app[web.1]:
2013-12-27T08:50:58.823687+00:00 app[web.1]: throw er; // Unhandled 'error' event
2013-12-27T08:50:58.826090+00:00 app[web.1]: at null.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:540:74)
2013-12-27T08:50:58.826090+00:00 app[web.1]: at Socket.EventEmitter.emit (events.js:95:17)
2013-12-27T08:50:58.826090+00:00 app[web.1]: at EventEmitter.emit (events.js:98:17)
2013-12-27T08:50:58.826090+00:00 app[web.1]: Error: failed to connect to [localhost:27017]
2013-12-27T08:50:58.826090+00:00 app[web.1]: at EventEmitter.emit (events.js:106:17)
2013-12-27T08:50:58.826090+00:00 app[web.1]: at null.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:140:15)
2013-12-27T08:50:58.826090+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:478:10)
2013-12-27T08:50:58.826090+00:00 app[web.1]: at net.js:426:14
2013-12-27T08:50:58.826090+00:00 app[web.1]: at process._tickCallback (node.js:415:13)
2013-12-27T08:50:59.977120+00:00 heroku[web.1]: Process exited with status 8
2013-12-27T08:50:59.998919+00:00 heroku[web.1]: State changed from starting to crashed
2013-12-27T08:57:32.833792+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/ host=hipsterlove.herokuapp.com fwd="174.251.209.112" dyno= connect= service= status=503 bytes=
2013-12-27T09:01:35.385263+00:00 heroku[web.1]: Starting process with command `node app.js`
2013-12-27T09:01:36.760608+00:00 app[web.1]: events.js:72
2013-12-27T09:01:36.758539+00:00 app[web.1]: Express server listening on port 4749
2013-12-27T09:01:36.760891+00:00 app[web.1]: throw er; // Unhandled 'error' event
2013-12-27T09:01:36.760257+00:00 app[web.1]:
2013-12-27T09:01:36.761120+00:00 app[web.1]: ^
2013-12-27T09:01:36.763625+00:00 app[web.1]: at EventEmitter.emit (events.js:98:17)
2013-12-27T09:01:36.763625+00:00 app[web.1]: at EventEmitter.emit (events.js:106:17)
2013-12-27T09:01:36.763625+00:00 app[web.1]: Error: failed to connect to [localhost:27017]
2013-12-27T09:01:36.763625+00:00 app[web.1]: at null.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:540:74)
2013-12-27T09:01:36.763625+00:00 app[web.1]: at null.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:140:15)
2013-12-27T09:01:36.763625+00:00 app[web.1]: at net.js:426:14
2013-12-27T09:01:36.763625+00:00 app[web.1]: at process._tickCallback (node.js:415:13)
2013-12-27T09:01:36.763625+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:478:10)
2013-12-27T09:01:36.763625+00:00 app[web.1]: at Socket.EventEmitter.emit (events.js:95:17)
2013-12-27T09:01:37.955733+00:00 heroku[web.1]: Process exited with status 8
2013-12-27T09:01:37.987115+00:00 heroku[web.1]: State changed from starting to crashed
2013-12-27T09:01:33.253022+00:00 heroku[web.1]: State changed from crashed to starting
It looks like in prod you are referencing your local mongoDB instance:
Error: failed to connect to [localhost:27017]
I imagine you've hardcoded these values...
The recommended way of handling connection strings like this that change from environment to environment is to use environment variables (http://12factor.net/config).
So you'll need to do the following (if you haven't already).
1) Set up a mongo instance in the cloud (MongoHQ, mongoLab, etc) (heroku makes this fairly easy).
2) If heroku hasn't set the env variables for the connection strings you'll need to do that.
To list your env vars (enter this on the command line):
heroku config -a name_of_your_heroku_app
To set your env vars:
heroku config:set MONGO_URL=somethingGoesHere -a name_of_your_heroku_app
Then in your node code you can reference that value like so:
var url = process.env.MONGO_URL;
To set your env variables locally, create a .env file in the root of your app. Running foreman start will run your procfile as well as loading the .env variables into memory.
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);
});
});
If the above answers fall a little short for anyone (they did for me) - try appending ?ssl=true to the end of your DATABASE_URL environment variable. Credit to the author of this answer. Best of luck.
You can use: heroku pg:info command to list all your databases. There you will find the exact databse url that you can use in your app - it should be something like that: HEROKU_POSTGRESQL_DBNAME_URL. This url can be used in node.js application:
pg.connect(process.env.HEROKU_POSTGRESQL_DBNAME_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);
});
});
Try heroku pg:promote like so:
heroku pg:promote HEROKU_POSTGRESQL_WHATEVER_URL
This should set the DATABASE_URL variable on Heroku's side.
I would not recommend hardcoding the URL, because you may be willing to create staging app with the same code.
You may use nconf to make sure Heroku's environment variables (stored in a .env file, read by foreman when launching your app with Procfile).
You may create a config.js file gathering your variables:
const nconf = require('nconf')
module.exports = nconf.argv().env()
Then use it to retrieve your variables:
import config from './config'
config.get('HEROKU_POSTGRESQL_DBNAME_URL')
Note that you could use the exact same setup as Heroku on your local machine. All you need is to create a Procfile to describe how to start your server and a .env file with you local variables (e.g. your local postgresql database url). Check https://github.com/strongloop/node-foreman
I had the same issue while connecting to my Nestjs(Nodejs app) to Heroku PostgreSQL database. I fixed it by adding
SSL: true property in the ormconfig.json file
So use the following code snippet for nodejs app:
const { Client } = require('pg');
const client = new Client({
connectionString: process.env.DATABASE_URL,
ssl: true,
});
client.connect();
client.query('SELECT table_schema,table_name FROM information_schema.tables;', (err, res) => {
if (err) throw err;
for (let row of res.rows) {
console.log(JSON.stringify(row));
}
client.end();
});
and for java app:
#Configuration
public class MainConfig {
#Bean
public BasicDataSource dataSource() throws URISyntaxException {
URI dbUri = new URI(System.getenv("DATABASE_URL"));
String username = dbUri.getUserInfo().split(":")[0];
String password = dbUri.getUserInfo().split(":")[1];
String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':' + dbUri.getPort() + dbUri.getPath() + "?sslmode=require";
BasicDataSource basicDataSource = new BasicDataSource();
basicDataSource.setUrl(dbUrl);
basicDataSource.setUsername(username);
basicDataSource.setPassword(password);
return basicDataSource;
}}
Note: Follow this link for other technologies database connection details.