728x90
728x90

PostgreSQL 설치 및 사용방법

들어가며

  • Node.js에서 npm을 이용하여 PostgreSQL을 설치하고, 사용하는 방법을 알아보자.

 

PostgreSQL 설치하기

  • 터미널에 npm을 이용하여 아래의 명령으로 PostgreSQL을 설치할 수 있다.
$ npm install pg

 

사용 방법

  • db.connect()를 이용하여 PostgreSQL DB에 연동한 후, db.end() 명령을 이용하여 DB 연동을 해제한다.
import pg from "pg";
const db = new.pg.Client({
user: "postgres",
host: "localhost",
database: "world",
password: "123456", // 설치 시 설정했던 암호
port: 5432, // 설치 시 설정했던 포트 번호
});
db.connect(); // DB 연결
db.query("SELECT * FROM table", (err, res) => {
if (err) {
console.error("Error executing query", err.stack);
}
else {
quiz = res.rows;
}
db.end(); // DB 연결 해제
});

 

참고 사이트

 

pg

PostgreSQL client - pure javascript & libpq with the same API. Latest version: 8.11.3, last published: 3 months ago. Start using pg in your project by running n±ipg. There are 9024 other projects in the npm registry using pg.

www.npmjs.com

 

728x90
728x90

PostgreSQL 설치 및 사용방법들어가며PostgreSQL 설치하기사용 방법참고 사이트