Created
September 7, 2018 01:20
-
-
Save agmezr/1a33f036a49c371b44649890886f0918 to your computer and use it in GitHub Desktop.
An example of connecting a postgres database with node using node-postgres
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
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