Development

Node.js의 pg 모듈을 이용한 PostgreSQL 연결

아무로레이·2022년 7월 1일·조회 2,354

1. 배경

Node.js 기반으로 PostgreSQL 연동하여 작업 실행.


2. pg 설치

npm install pg

3. Connection Pool 

const { Pool } = require('pg')

const pool = new Pool({
  user: 'malladmin',
  host: '[DB주소]',
  database: 'malldb',
  password: 'mall00',
  port: 5432,
})

4. 데이터 삽입 테스트

const add_sql = 'INSERT INTO data_log ( currenttime, service, data_name, value ) VALUES ( default, $1, $2, $3 )';

pool.query(add_sql, ['test1', 'test2', 'test3'], (err, res) => {
  if (err) {
    throw err
  }
  console.log('response:', res.rows[0])
})

댓글 0

로그인 후 댓글을 남길 수 있습니다.

아직 댓글이 없습니다.