UGA Boxxx

つぶやきの延長のつもりで、知ったこと思ったこと書いてます

【Sequelize】SequelizeでPostgresqlに接続する

PromiseベースのNode.js ORMのSequelizeを使ってPostgresqlに接続してみる sequelize.org

$ npm install sequelize
$ npm install pg pg-hstore

DB接続

import { Sequelize } from 'sequelize';

const sequelize = new Sequelize('postgres://user:pass@example.com:5432/dbname')

検証

.authenticate()を使うと接続ができているかが確認できる

try {
  await sequelize.authenticate();
  console.log('Connection has been established successfully.');
} catch (error) {
  console.error('Unable to connect to the database:', error);
}