Skip to content

Instantly share code, notes, and snippets.

@davehax
Created July 13, 2017 08:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davehax/0ec9f5f802579fbf5da6817fd8cd5f47 to your computer and use it in GitHub Desktop.
Save davehax/0ec9f5f802579fbf5da6817fd8cd5f47 to your computer and use it in GitHub Desktop.
// ... other includes
// FIRST - npm install pg
var pg = require('pg');
// SECOND - get the full postgresql connection string.
// You'll have to manually split it up into the various components required to build the
// connection config object.
// e.g. the connection string postgres://randomname:bigasspassword@some-host.compute-x.amazonaws.com:5432/lotsofcharacters
// becomes:
var pool = new pg.Pool({
user: 'randomname', //env var: PGUSER
database: 'lotsofcharacters', //env var: PGDATABASE
password: 'bigasspassword', //env var: PGPASSWORD
host: 'some-host.compute-x.amazonaws.com', // Server hosting the postgres database
port: 5432, //env var: PGPORT
max: 10, // max number of clients in the pool
idleTimeoutMillis: 30000, // how long a client is allowed to remain idle before being closed
})
// ... other parts of the tutorial code
app.get('/db', function (request, response) {
pool.query('SELECT * FROM test_table', function (err, result) {
if (err) {
console.error(err); response.send("Error " + err);
}
else {
response.render('pages/db', { results: result.rows });
}
});
});
// ... other parts of the tutorial code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment