IT/Node.js

Node.js Sequelize Mysql -> Postgresql

1am_wish 2024. 1. 7. 20:27
728x90
반응형

기존에 mysql로 dbms를 사용하여 서비스를 만들어둔 상태인데, postgresql의 장점이 만들고자 하는 서비스에 적합하다고 생각하여 바꾸게되었다!

똑같이 Sequelize 사용이 가능하며 연결 옵션만 수정해주면 바로 연결과 sync가 가능했다.

 

기존에 사용하던 mysql과 postgresql 둘다 AWS RDS를 사용했고, 옵션을 크게 바꿔준 것은 없었다.

 

기존 : Mysql 사용

// config

    "mysql": {
        "host": "", //mysql host
        "username": "", //mysql user
        "password": "", //mysql password
        "database": "", //mysql database
        "dialect": "mysql",
        "timezone": "+09:00"
    },
// sequelize.js

import Sequelize from 'sequelize';
import config from 'config';
import {models} from '../src/models/index.js';

const sequelize = new Sequelize(
    config.get('mysql.database'),
    config.get('mysql.username'),
    config.get('mysql.password'),
    {
      host: config.get('mysql.host'),
      dialect: config.get('mysql.dialect'),
      timezone: config.get('mysql.timezone'), // 예시: 대한민국 시간대에 맞는 옵션 설정
    },
);

 

변경 : Postgresql 사용

// config

    "postgres": {
        "host": "", //postgres host
        "username": "", //postgres user
        "password": "", //postgres password
        "database": "", //postgres database
        "dialect": "postgres",
        "timezone": "+09:00"
    },
// sequelize.js

import Sequelize from 'sequelize';
import config from 'config';
import {models} from '../src/models/index.js';

const sequelize = new Sequelize(
    config.get('postgres.database'),
    config.get('postgres.username'),
    config.get('postgres.password'),
    {
      host: config.get('postgres.host'),
      dialect: config.get('postgres.dialect'),
      timezone: config.get('postgres.timezone'), // 예시: 대한민국 시간대에 맞는 옵션 설정
      dialectOptions: {
        ssl: {
          require: true,
          rejectUnauthorized: false,
        },
      },
    },
);

특징적으로는 dialectOptions 옵션이 추가됐다. 저 옵션이 없으면 아래 에러와 같이 SSL에서 막혀서 연결이 되지 않는다

Unable to connect to the database: ConnectionError [SequelizeConnectionError]: no pg_hba.conf entry for host "222.109.176.119", user "test", database "postgres", no encryption at Client._connectionCallback (C:\Users\sowon\project\kkosunae\node_modules\sequelize\lib\dialects\postgres\connection-manager.js:143:24) at Client._handleErrorWhileConnecting (C:\Users\sowon\project\kkosunae\node_modules\pg\lib\client.js:327:19) at Client._handleErrorMessage (C:\Users\sowon\project\kkosunae\node_modules\pg\lib\client.js:347:19) at Connection.emit (node:events:513:28) at Connection.emit (node:domain:489:12) at C:\Users\sowon\project\kkosunae\node_modules\pg\lib\connection.js:117:12 at Parser.parse (C:\Users\sowon\project\kkosunae\node_modules\pg-protocol\dist\parser.js:40:17) at Socket.<anonymous> (C:\Users\sowon\project\kkosunae\node_modules\pg-protocol\dist\index.js:11:42) at Socket.emit (node:events:513:28) at Socket.emit (node:domain:489:12) { 
...

rejectUnauthorizedfalse로 설정하면, 서버가 self-signed 인증서를 사용하는 경우에도 연결 가능해진다.

보안상의 이유로 실제 운영에서는 rejectUnauthorizedtrue로 설정하고 서버의 인증서를 신뢰할 수 있는 것으로 구성하는 것이 좋다!

 

시행착오

문제 : 처음에 Postgresql을 연결하는 과정에서 위 에러가 발생

시도1 : RDS로 만든 postgresql은 pg_hba.conf를 직접 수정하기 번거로워, 파라미터 그룹으로 설정하려함.


여러 레퍼런스를 찾아서 RDS의 파라미터 그룹을 생성하고 rds.force_ssl을 0으로 설정하는 등 적용을 해봤지만 에러는 사라지지 않음

시도2 : 파라미터 그룹 원복 후 dialectOptions 을 추가하여 재시도. -> 성공 !

728x90
반응형