The recommended way to connect to postgres is via the client
node-postgres.
Install it by running
npm install pg
.
elephantsql.js
You run the code by typing the following in terminal:
node filename.js
var pg = require('pg');
//or native libpq bindings
//var pg = require('pg').native
var conString = "INSERT_YOUR_POSTGRES_URL_HERE" //Can be found in the Details page
var client = new pg.Client(conString);
client.connect(function(err) {
if(err) {
return console.error('could not connect to postgres', err);
}
client.query('SELECT NOW() AS "theTime"', function(err, result) {
if(err) {
return console.error('error running query', err);
}
console.log(result.rows[0].theTime);
// >> output: 2018-08-23T14:02:57.117Z
client.end();
});
});