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 . There are 9024 other projects in the npm registry using pg.
www.npmjs.com
728x90
728x90
'Programming > Node.js' 카테고리의 다른 글
[Node.js] Nodemailer 라이브러리 (0) | 2024.11.26 |
---|---|
[Node.js] 모듈 설치 시 의존성 문제 해결하기 (npm-check-updates) (1) | 2023.11.27 |
[Node.js] EJS(Embedded JavaScript) (0) | 2023.11.09 |
[Node.js] morgan 패키지 (0) | 2023.11.09 |
[Node.js] body-parser 패키지 (0) | 2023.11.09 |
[Node.js] 노드몬(nodemon) 패키지 (0) | 2023.11.08 |
[Node.js] Express.js (0) | 2023.11.08 |
[Node.js] URL QR 코드 생성기 만들기 (0) | 2023.11.07 |