DEV/ETC

PostgreSQL - psql로 csv에서 데이터 입력하기

Windy Miky 2020. 10. 8. 16:41
반응형

 출처 : https://images.app.goo.gl/YS4NcTrVkZ2bYqLG8

아직 PostgreSql이랑 안친하다. 모든 게 버벅버벅... 그래도 일은 해야하니까~

일단 csv에서 데이터를 넣어야 한다. 팀원에게 넣어달라했는데 너무 바쁘단다. 일단 해보자.

우리는 NestJS, TypeORM을 사용 중이다.

그래서 일단 테이블은 Entity로 생성한다. 공유를 위하여~~

import { IETFLanguageCode } from "@gi/glodex";
import { Timestamp } from "src/shared/entity/base.entity";
import { Column, Entity, PrimaryColumn } from "typeorm";
import { LangRateEntityType } from "../project.type";

@Entity()
export class LangRate extends Timestamp {
    @Column('varchar', { length: 10, nullable: false })
    code: keyof typeof IETFLanguageCode

    @Column('varchar', { length: 10, nullable: false })
    type: keyof typeof LangRateEntityType

    @Column('decimal')
    client: number
    @Column('decimal')
    pro: number
}

DB Migration을 한 후 터미널로 달려가 psql로 DB에 접속한다.

테이블이 예쁘게 생성되었다.

                                        Table "public.lang_rate"
  Column   |            Type             | Collation | Nullable |                Default
-----------+-----------------------------+-----------+----------+---------------------------------------
 id        | integer                     |           | not null | nextval('lang_rate_id_seq'::regclass)
 createdAt | timestamp without time zone |           | not null | now()
 updatedAt | timestamp without time zone |           | not null | now()
 deletedAt | timestamp without time zone |           |          |
 code      | character varying(10)       |           | not null |
 type      | character varying(10)       |           | not null |
 client    | numeric                     |           | not null |
 pro       | numeric                     |           | not null |
Indexes:
    "PK_790e16e09ffe6af9e5a43f8af59" PRIMARY KEY, btree (id)

이제 대망의 csv에서 데이터 넣기!!

\copy lang_rate_entity(code, name, type, client, pro) from '/Users/miky/Downloads/LangRate.csv' DELIMITER ',' CSV HEADER;

그리고 데이터 확인해보면 아주아주 예쁘게도 잘 들어와 있다!!

반응형