Skip to content

Instantly share code, notes, and snippets.

@Qarun-Qadir-Bissoondial
Last active May 10, 2020 22:06
Show Gist options
  • Save Qarun-Qadir-Bissoondial/cb31a23737844123581a8245aac2e911 to your computer and use it in GitHub Desktop.
Save Qarun-Qadir-Bissoondial/cb31a23737844123581a8245aac2e911 to your computer and use it in GitHub Desktop.
Simple Postgres connection with Sequelize
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