Node.js/Express

DB - Postgre

jm_p_op 2024. 4. 5. 00:24

install

npm install --save sequelize
npm install --save sequelize-cli
npx sequelize-cli init

DB연동 설정하기

config/config.json

  • process.env.NODE_ENV의 설정에 따라 DB변동 시킬수 있음
{
  "development": {
    "username": "root",
    "password": null,
    "database": "database_development",
    "host": "127.0.0.1",
    "dialect": "mysql"
  },
  "test": {
    "username": "root",
    "password": null,
    "database": "database_test",
    "host": "127.0.0.1",
    "dialect": "mysql"
  },
  "production": {
    "username": "root",
    "password": null,
    "database": "database_production",
    "host": "127.0.0.1",
    "dialect": "mysql"
  }
}

gitignore설정

  • .config

연동확인하기

//db_conection_test.js
const models = require("./models/index")
models.sequelize.sync().then(()=>{
    console.log("연동")
}).catch(err=>{
    console.log("err");
console.log(err)
})

 

sercret_key를 config에서 사용하지 않는다면

index.js에서 config 수정하면 된다