Created
November 15, 2018 02:24
-
-
Save rublev/9654670c40faebdbc30341bb287b1842 to your computer and use it in GitHub Desktop.
node.js+postgresqlをherokuで動かすメモ ref: http://qiita.com/ta9to/items/f7b55246cfe42ed14743
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ heroku addons:add heroku-postgresql:dev | |
$ heroku addons:docs heroku-postgresql # ブラウザでドキュメント開く | |
$ heroku pg:wait # データベースの準備ができるまで5分くらいかかる。その進捗ステータスを表示する | |
$ heroku config | grep HEROKU_POSTGRESQL | |
$ heroku pg:promote HEROKU_POSTGRESQL_COPPER_URL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ heroku pg:psql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ brew install postgresql | |
$ initdb /usr/local/var/postgres -E utf8 | |
$ psql -l | |
$ heroku pg:pull HEROKU_POSTGRESQL_COPPER_URL mylocaldb --app polar-plateau-2763 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ npm install pg --save | |
$ export DATABASE_URL=tcp://localhost:5432/mylocaldb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ foreman start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// web.js | |
var express = require("express"); | |
var logfmt = require("logfmt"); | |
var app = express(); | |
var pg = require('pg'); | |
var hoge; | |
pg.connect(process.env.DATABASE_URL, function(err, client, done) { | |
client.query('SELECT * FROM post', function(err, result) { | |
done(); | |
if(err) return console.error(err); | |
hoge = result.rows; | |
}); | |
}); | |
app.use(logfmt.requestLogger()); | |
app.get('/', function(req, res) { | |
res.send(hoge); | |
}); | |
var port = Number(process.env.PORT || 5000); | |
app.listen(port, function() { | |
console.log("Listening on " + port); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ git add . | |
$ git commit "jsonhakudake" | |
$ git push heroku master | |
$ http http://polar-plateau-2763.herokuapp.com/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var pg = require('pg'); | |
pg.connect(process.env.DATABASE_URL, function(err, client, done) { | |
client.query('SELECT * FROM post', function(err, result) { | |
done(); | |
if(err) return console.error(err); | |
console.log(result.rows); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment