postgresql 줄바꿈 엔터 추가하기

카초·2023년 12월 1일

나는 장고 웹개발 프로젝트 중이고 postgresql을 처음 써보는 중이다.

UPDATE public.community_post
SET content = REGEXP_REPLACE(content, '다\.', E'다.\n', 'gi');
UPDATE public.community_post
SET content = REGEXP_REPLACE(content, '해당 게시글 바로가기', E'\n해당 게시글 바로가기', 'i')

postgresql에서 content 본문 내용에 \n 줄바꿈이 안 되어 있어 가독성이 떨어지는 문제가 있었다. 그래서 임시방편으로 생각한 것이 "다." 뒤에 엔터를 누르는 것. 그리고 늘 본문 마지막에 오도록 설정해둔 "해당 게시글 바로가기" 앞에 엔터 누르기.

UPDATE community_post
SET context = REGEXP_REPLACE(context, '다\.', '다.\n', 'g');

처음엔 이 코드를 썼는데 이렇게 했더니 진짜 "\n"가 문자로 입력되더라;; 같은 당혹스러움을 겪은 사람들은 상단의 코드를 참고하길.


오늘 저장한 데이터에 한하여 수정하기

UPDATE public.community_post
SET content = REGEXP_REPLACE(content, '다\.', E'다.\n', 'gi')
WHERE DATE_TRUNC('day', "time") = CURRENT_DATE;

UPDATE public.community_post
SET content = REGEXP_REPLACE(content, '해당 게시글 바로가기', E'\n해당 게시글 바로가기', 'i')
WHERE DATE_TRUNC('day', "time") = CURRENT_DATE;

중복되는 튜플은 title, content 기준으로 가장 오래된 하나만 남기기

WITH deduplicated_posts AS (
  SELECT
    title,
    content,
    ROW_NUMBER() OVER (PARTITION BY title, content ORDER BY time ASC) AS row_num
  FROM
    community_post
)
DELETE FROM community_post
WHERE (title, content) IN (
  SELECT
    title,
    content
  FROM
    deduplicated_posts
  WHERE
    row_num > 1
);

<데이터 저장명령어>

  1. Postgresql -> json
    python -Xutf8 manage.py dumpdata --exclude=contenttypes --exclude=auth.Permission > postgresql_data.json --indent 4

  2. json -> Postgresql
    python manage.py loaddata postgresql_data.json

0개의 댓글