Skip to content

Instantly share code, notes, and snippets.

@agmezr
Created September 7, 2018 01:20
Show Gist options
  • Save agmezr/1a33f036a49c371b44649890886f0918 to your computer and use it in GitHub Desktop.
Save agmezr/1a33f036a49c371b44649890886f0918 to your computer and use it in GitHub Desktop.
An example of connecting a postgres database with node using node-postgres
const { Pool } = require('pg');
const { DATABASE_URL } = process.env;
// if the url for the database is in process use that,
// usually when using heroku or similar to deploy
if (DATABASE_URL){
console.log("Using database url");
var pool = new Pool({
connectionString: DATABASE_URL
});
}else{
console.log("Using database info");
var pool = new Pool({
user: 'username',
host: 'localhost',
database: 'database_name',
password: 'my_password',
port: 5432
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment