데이터베이스 운영
연결 정보
| 항목 | 기본값 |
|---|---|
| 호스트 | localhost |
| 포트 | 5432 |
| 데이터베이스 | hereby_platform |
| 사용자 | hereby_admin |
| DBMS | PostgreSQL 16 (TimescaleDB + pgvector) |
pgAdmin 접속
- 브라우저에서
http://localhost:5050접속 - 로그인:
admin@hereby.platform/admin123 - 서버 추가 시 호스트를
postgres(Docker 네트워크명)로 설정
마이그레이션
마이그레이션 상태 확인
bash
pnpm --filter @hereby/database typeorm migration:show -d src/config/data-source.ts실행된 마이그레이션은 [X], 미실행은 [ ]로 표시됩니다.
마이그레이션 실행
bash
# Makefile 사용
make db-migrate
# 또는 직접 실행
pnpm --filter @hereby/database typeorm migration:run -d src/config/data-source.ts마이그레이션 되돌리기
bash
pnpm --filter @hereby/database typeorm migration:revert -d src/config/data-source.tsWARNING
마이그레이션 되돌리기는 가장 최근 마이그레이션 하나만 되돌립니다. 여러 개를 되돌리려면 반복 실행하세요.
새 마이그레이션 생성
엔티티 변경 후 자동 생성:
bash
pnpm --filter @hereby/database typeorm migration:generate \
-- src/migrations/DescriptiveName \
-d src/config/data-source.ts빈 마이그레이션 수동 생성:
bash
pnpm --filter @hereby/database typeorm migration:create \
-- src/migrations/DescriptiveName백업 및 복구
데이터베이스 백업
bash
# 전체 백업
docker exec hereby_postgres pg_dump \
-U hereby_admin \
-d hereby_platform \
-F c \
-f /tmp/backup_$(date +%Y%m%d_%H%M%S).dump
# 백업 파일을 호스트로 복사
docker cp hereby_postgres:/tmp/backup_*.dump ./backups/데이터만 백업
bash
docker exec hereby_postgres pg_dump \
-U hereby_admin \
-d hereby_platform \
--data-only \
-F c \
-f /tmp/data_backup.dump복구
bash
# 데이터베이스 복구
docker exec -i hereby_postgres pg_restore \
-U hereby_admin \
-d hereby_platform \
-c \
/tmp/backup.dumpDANGER
복구 시 -c 옵션은 기존 데이터를 삭제합니다. 반드시 현재 데이터를 먼저 백업하세요.
자동 백업 스케줄 (crontab)
bash
# 매일 새벽 2시 백업
0 2 * * * docker exec hereby_postgres pg_dump -U hereby_admin -d hereby_platform -F c -f /backups/daily_$(date +\%Y\%m\%d).dumpPostgreSQL 셸 접속
bash
# Makefile 사용
make db-shell
# 또는 직접 접속
docker exec -it hereby_postgres psql -U hereby_admin -d hereby_platform유용한 SQL 쿼리
sql
-- 테이블 목록 확인
\dt
-- 테이블 크기 확인
SELECT schemaname, tablename,
pg_size_pretty(pg_total_relation_size(schemaname || '.' || tablename))
FROM pg_tables
WHERE schemaname = 'public'
ORDER BY pg_total_relation_size(schemaname || '.' || tablename) DESC;
-- 활성 연결 수 확인
SELECT count(*) FROM pg_stat_activity WHERE state = 'active';
-- 느린 쿼리 확인 (1초 이상)
SELECT pid, now() - pg_stat_activity.query_start AS duration, query
FROM pg_stat_activity
WHERE state != 'idle'
AND now() - pg_stat_activity.query_start > interval '1 seconds'
ORDER BY duration DESC;시드 데이터
테스트 데이터 입력:
bash
make db-seed시드 후 사용 가능한 계정:
| 역할 | 이메일 | 비밀번호 |
|---|---|---|
| 관리자 | admin@hereby.com | admin123 |
| 개발자 | dev.lee@hereby.com | dev123 |
| HR 관리자 | hr.kim@hereby.com | hr123 |
| 영업 | sales.park@hereby.com | sales123 |