Postgresql 한글 정렬 관련 문제

훈이·2023년 10월 11일
0

nestjs + postgresql + typeorm으로 개인 프로젝트를 진행하던 중이였는데 카테고리를 전체 조회하는 로직에서 문제가 생겼다.

async findAll() {
    const categories = await this.categoryRepository.find({
      where: {
        parent: IsNull(),
      },
      relations: ['children'],
      order: {
        name: 'asc',
        children: {
          name: 'asc',
        },
      },
    });

    return categories;
  }
  [
      {
          "id": "0c0645da-ed48-4723-b0a1-a444145f0982",
          "name": "개발/프로그래밍",
          "created_at": "2023-08-21T07:31:09.297Z",
          "updated_at": "2023-08-21T07:31:09.297Z",
          "fk_parent_category_id": null,
          "children": [
              {
                  "id": "a1e8875e-bf5c-4f06-beaa-9187e4537480",
                  "name": "백엔드",
                  "created_at": "2023-08-21T07:31:09.297Z",
                  "updated_at": "2023-08-21T07:31:09.297Z",
                  "fk_parent_category_id": "0c0645da-ed48-4723-b0a1-a444145f0982"
              },
              {
                  "id": "8776688a-2744-4687-b664-006ef8b9861b",
                  "name": "풀스택",
                  "created_at": "2023-08-21T07:31:09.297Z",
                  "updated_at": "2023-08-21T07:31:09.297Z",
                  "fk_parent_category_id": "0c0645da-ed48-4723-b0a1-a444145f0982"
              },
              {
                  "id": "d8b22a7e-37ba-4193-b228-f63b2491b72a",
                  "name": "프론트엔드",
                  "created_at": "2023-08-21T07:31:09.297Z",
                  "updated_at": "2023-08-21T07:31:09.297Z",
                  "fk_parent_category_id": "0c0645da-ed48-4723-b0a1-a444145f0982"
              },
              {
                  "id": "c25b6b8e-9166-4389-8b16-28804af221a2",
                  "name": "알고리즘/자료구조",
                  "created_at": "2023-08-21T07:31:09.297Z",
                  "updated_at": "2023-08-21T07:31:09.297Z",
                  "fk_parent_category_id": "0c0645da-ed48-4723-b0a1-a444145f0982"
              },
              {
                  "id": "1afc46e1-65ec-44fb-a256-ec5943254477",
                  "name": "모바일 앱 개발",
                  "created_at": "2023-08-21T07:31:09.297Z",
                  "updated_at": "2023-08-21T07:31:09.297Z",
                  "fk_parent_category_id": "0c0645da-ed48-4723-b0a1-a444145f0982"
              }
          ]
      },
      {
          "id": "8ef0f3c5-c640-4cbb-9fba-eab6b249f859",
          "name": "게임 개발",
          "created_at": "2023-08-21T07:31:09.297Z",
          "updated_at": "2023-08-21T07:31:09.297Z",
          "fk_parent_category_id": null,
          "children": [
              {
                  "id": "48685f11-7f96-4098-98c2-1545c77e17bd",
                  "name": "게임 프로그래밍",
                  "created_at": "2023-08-21T07:31:09.297Z",
                  "updated_at": "2023-08-21T07:31:09.297Z",
                  "fk_parent_category_id": "8ef0f3c5-c640-4cbb-9fba-eab6b249f859"
              }
          ]
      },
      {
          "id": "36b5d240-c0e5-4208-82ae-be5e61517bf9",
          "name": "데이터 사이언스",
          "created_at": "2023-08-21T07:31:09.297Z",
          "updated_at": "2023-08-21T07:31:09.297Z",
          "fk_parent_category_id": null,
          "children": [
              {
                  "id": "08650064-250b-48ee-8103-fd763185e7cb",
                  "name": "데이터 분석",
                  "created_at": "2023-08-21T07:31:09.297Z",
                  "updated_at": "2023-08-21T07:31:09.297Z",
                  "fk_parent_category_id": "36b5d240-c0e5-4208-82ae-be5e61517bf9"
              }
          ]
      }
  ]

위의 코드와 정렬 방식을 보면 나는 메인 카테고리와 서브 카테고리의 정렬을 'ASC'로 하였지만 전혀 정렬이 되지 않은걸 볼 수 있다.
그래서 구글링을 통해 알아본 결과 Postgresql의 collate옵션 설정이 문제였다.
collate는 데이터베이스의 정렬 순서를 결정한다고 한다.

그래서 나의 문제를 해결하기 위해서는 내 데이터베이스의 collate 설정이 ko_KR.UTF-8 또는 en_US.utf8로 되어있는 것을 C로 변경하면 된다.

하지만 데이터베이스의 collate 설정 변경은 초기 데이터베이스 생성시에만 가능하기 때문에

psql 접속 후

$ DROP DATABASE 데이터베이스이름; 
$ CREATE DATABASE 데이터베이스이름 LC_COLLATE 'C';

이렇게 데이터베이스를 삭제 후 collate 설정을 추가해 만들어주었다.

하지만..... 쉽게 넘어가지 않았다......

이런 오류가 나오는데 데이터베이스를 기본적으로 생성할 때 collate 설정이 이미 초기화 되어있는 template1을 복제해 생성한다고 한다. 그래서 우리는 collate 설정이 초기화 되어있지않은 template0을 사용해야한다.

$ CREATE DATABASE 데이터베이스이름 TEMPLATE template0 LC_COLLATE 'C';  

이렇게 명령어를 쳐주니 collate가 C로 된 데이터베이스가 생성되었고 정렬문제가 해결되었다.

참고한 사이트 : https://jupiny.com/2016/12/12/sort-korean-in-postgresql/

profile
백엔드 개발자가 되자!

0개의 댓글