[Express] 整合 postgreSQL 與 heroku
前置動作
# 安裝 pg 套件
$ npm install pg
# 在 heroku 上新增 postgreSQL 的 database
$ heroku addons:create heroku-postgresql:hobby-dev
Sequelize Config 設定
設定 Express 專案中 Sequelize 的 config
// config/config.json
{
//...
"production": {
"use_env_variable": "DATABASE_URL",
"protocol": "postgres",
"dialect": "postgres",
"dialectOptions": {
"ssl": {
"require": true,
"rejectUnauthorized": false
}
}
}
}
- 透過 heroku 產生 PostgreSQL 的資料庫後,在 Heroku 的環境變數中會自動把
DATABASE_URL
帶入 postgreSQL 資料庫的 URI,因此在設定檔中可以不用填寫帳號密碼,直接使用use_env_variable: "DATABASE_URL"
。 - 在
dialectOptions.ssl
中有一個rejectUnauthorized
選項,在未設定 SSL 前需要使用這個選項。如果沒有加上這個選項的話,在 node-postgres^8.0.0 之後會出現 SequelizeConnectionError: self signed certificate。