node js에 postgreSQL 연동

yunji·2023년 1월 26일
0

node js

목록 보기
2/9
post-thumbnail

설정 환경

ubuntu 20.04
AWS EC2

1. postgreSQL 설치

1) posgreSQL 설치

sudo apt install posgresql

2) postgresql 라이브 서버 설치

sudo apt install postgresql-contrib

3) 권한 바꾸기

 sudo su

4) posgres로 계정 변환하기

su postgres

5) postgresql에 접근 하기

psql

postgres=# 이와 같이 나오면 제대로 접근 완료.

6) ubuntu라는 계정에 로그인 권한 주기

postgres=# CREATE USER ubuntu WITH  LOGIN PASSWORD'1234';

7) 데이터베이스 생성

CREATE DATABASE mydb;

8) 생성한 데이터베이스에 권한 주기

postgres=# GRANT ALL PRIVILEGES ON DATABASE mydb TO ubuntu;

9) 스키마 생성

project -> 스키마 이름

postgres=# CREATE SCHEMA project;

10) table 생성

account -> table 이름

postgres=# CREATE TABLE project.account(id varchar, pw varchar);

2. npm으로 pg 설치

express는 postgreSQL을 바로 알아 볼 수 없다. pg 패키지를 사용하여
서로 알아 보도록 해준다.

npm install pg

3. node js에서 psql 사용하기

먼저 psql.js 파일을 생성 해준다. 그 다음 다음과 같이 작성 한다.


const pgInit = {
    "user": PG_USER,//pg 사용하는 사용자 
    "host": locallhost,
    "database": PG_DATABASE_NAME//db이름 
    "password": PG_PASSWORD,//db에 설정해준 비밀번호
    "port": 5432// pg 포트번호
}

module.exports = pgInit

이렇게 psql.js 파일 까지 만들고 나면 node js에서 postgreSQL을 사용 할 수 있다.

profile
웹 개발

0개의 댓글