문제 상황

pg_dumpall을 이용해 full_backup.sql을 생성하고 이를 새로 배포한 PostgreSQL에 덮어쓰는 순간 에러가 발생함

Error Logs 1

ERROR: database "affine_db" does not exist

기존 백업 명령어에서 사용한 옵션이 문제가 되었다. 새로운 PostgreSQL을 배포할 경우 DB가 없을 수 있는데 --if-exists가 없어서 에러가 발생한다.

  • Original
docker exec -t <postgres-container> pg_dumpalll -c -U postgres > full_backup.sql
  • Updated
docker exec postgresql pg_dumpall --clean --if-exists -U postgres > full_backup.sql

Error Logs 2

ERROR: current user cannot be dropped

full_backup.sql에 루트 유저인 postgres를 삭제하고 다시 생성하는 명령어가 포함되어 있어서 생기는 문제입니다.

Success

postgres를 설정하는 명령어를 삭제하고 재 복원한다.

  1. postgres 설정 명령어 삭제
sed -i.bak \
  -e '/^DROP ROLE IF EXISTS postgres;/d' \
  -e '/^DROP ROLE postgres;/d' \
  -e '/^CREATE ROLE postgres;/d' \
  -e '/^ALTER ROLE postgres /d' \
  full_backup.sql
  1. 재복원
kubectl exec -i -n core-infra postgresql-0 -- psql -U postgres -d postgres -v ON_ERROR_STOP=1 < full_backup.sql