Last active
May 10, 2020 22:06
-
-
Save Qarun-Qadir-Bissoondial/cb31a23737844123581a8245aac2e911 to your computer and use it in GitHub Desktop.
Simple Postgres connection with Sequelize
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 express = require('express'); | |
const { Sequelize } = require('sequelize'); | |
/* | |
new Sequelize takes in database name (postgres), username (postgres) and password (password). | |
It also takes in the host and the dialect of SQL (in this case, Postgres). | |
If you want another database name, you'll need to go inside PgAdmin and create a new database. | |
And then put that database name here | |
const sequelize_connection = new Sequelize('new_database_name', 'postgres', 'password', { | |
host: 'localhost', | |
dialect: 'postgres' | |
}); | |
*/ | |
const sequelize_connection = new Sequelize('postgres', 'postgres', 'password', { | |
host: 'localhost', | |
dialect: 'postgres' | |
}); | |
const app = express(); | |
const port = process.env.PORT || 5000; | |
sequelize_connection.authenticate() | |
.then(() => { console.log('Database connected!') }) | |
.catch(error => { console.error(error); }); | |
app.listen(port, () => { console.log(`Listening on port ${port}`); }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment