# πŸ—„οΈ λ°μ΄ν„°λ² μ΄μŠ€ μ„€μ • κ°€μ΄λ“œ ## πŸ“‹ κ°œμš” Travel Planner v2.0은 PostgreSQL을 μ‚¬μš©ν•˜μ—¬ λ©€ν‹° μ‚¬μš©μž μ—¬ν–‰ κ³„νšμ„ κ΄€λ¦¬ν•©λ‹ˆλ‹€. ## πŸš€ λΉ λ₯Έ μ‹œμž‘ ### 1. PostgreSQL μ„€μΉ˜ 확인 ```bash # PostgreSQL 버전 확인 psql --version # PostgreSQL μ„œλΉ„μŠ€ μƒνƒœ 확인 (macOS) brew services list | grep postgresql # PostgreSQL μ‹œμž‘ (macOS) brew services start postgresql ``` ### 2. λ°μ΄ν„°λ² μ΄μŠ€ 생성 ```bash # PostgreSQL 접속 psql postgres # λ°μ΄ν„°λ² μ΄μŠ€ 생성 CREATE DATABASE kumamoto_travel; # μ‚¬μš©μž 생성 (선택사항) CREATE USER kumamoto_user WITH PASSWORD 'your_password'; GRANT ALL PRIVILEGES ON DATABASE kumamoto_travel TO kumamoto_user; # μ’…λ£Œ \q ``` ### 3. ν™˜κ²½ λ³€μˆ˜ μ„€μ • ```bash # μ„œλ²„ λ””λ ‰ν† λ¦¬λ‘œ 이동 cd server # ν™˜κ²½ λ³€μˆ˜ 파일 생성 cp env.example .env # .env 파일 νŽΈμ§‘ nano .env ``` ### 4. .env 파일 μ˜ˆμ‹œ ```env # κΈ°λ³Έ μ„€μ • (둜컬 개발용) DATABASE_URL=postgresql://localhost:5432/kumamoto_travel # μ‚¬μš©μž 계정을 λ§Œλ“  경우 DATABASE_URL=postgresql://kumamoto_user:your_password@localhost:5432/kumamoto_travel # JWT μ‹œν¬λ¦Ώ (λžœλ€ν•œ λ¬Έμžμ—΄λ‘œ λ³€κ²½ν•˜μ„Έμš”) JWT_SECRET=your-super-secret-jwt-key-change-this-in-production-123456789 # 선택사항 GOOGLE_MAPS_API_KEY=your-google-maps-api-key PORT=3000 NODE_ENV=development ``` ## πŸ”§ μ„œλ²„ μ‹œμž‘ ### 1. μ˜μ‘΄μ„± μ„€μΉ˜ ```bash cd server npm install ``` ### 2. μ„œλ²„ μ‹€ν–‰ ```bash # 개발 λͺ¨λ“œ npm run dev # λ˜λŠ” 일반 λͺ¨λ“œ npm start ``` ### 3. μ„œλ²„ 확인 ```bash # ν—¬μŠ€ 체크 curl http://localhost:3000/health # λ°μ΄ν„°λ² μ΄μŠ€ μ—°κ²° ν…ŒμŠ€νŠΈ curl http://localhost:3000/api/setup/test-db # μ„€μ • μƒνƒœ 확인 curl http://localhost:3000/api/setup/status ``` ## πŸ“Š λ°μ΄ν„°λ² μ΄μŠ€ μŠ€ν‚€λ§ˆ ### μ£Όμš” ν…Œμ΄λΈ” - **users**: μ‚¬μš©μž 계정 (κ΄€λ¦¬μž/일반 μ‚¬μš©μž) - **travel_plans**: μ—¬ν–‰ κ³„νš (λ©€ν‹° λͺ©μ μ§€ 지원) - **day_schedules**: λ‚ μ§œλ³„ 일정 - **activities**: κ°œλ³„ ν™œλ™ - **share_links**: 곡유 링크 관리 - **trip_comments**: μ—¬ν–‰ κ³„νš λŒ“κΈ€ ### μŠ€ν‚€λ§ˆ μ—…λ°μ΄νŠΈ ```bash # κΈ°μ‘΄ 데이터가 μžˆλŠ” 경우 λ°±μ—… pg_dump kumamoto_travel > backup.sql # μƒˆ μŠ€ν‚€λ§ˆ 적용 psql kumamoto_travel < server/schema_v2.sql ``` ## πŸ” λ³΄μ•ˆ μ„€μ • ### JWT μ‹œν¬λ¦Ώ 생성 ```bash # 랜덀 μ‹œν¬λ¦Ώ 생성 (Node.js) node -e "console.log(require('crypto').randomBytes(64).toString('hex'))" # λ˜λŠ” OpenSSL μ‚¬μš© openssl rand -hex 64 ``` ### λ°μ΄ν„°λ² μ΄μŠ€ λ³΄μ•ˆ ```sql -- μ‚¬μš©μžλ³„ κΆŒν•œ μ„€μ • REVOKE ALL ON SCHEMA public FROM PUBLIC; GRANT USAGE ON SCHEMA public TO kumamoto_user; GRANT ALL ON ALL TABLES IN SCHEMA public TO kumamoto_user; GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO kumamoto_user; ``` ## πŸ› 문제 ν•΄κ²° ### μ—°κ²° 였λ₯˜ ```bash # PostgreSQL μ‹€ν–‰ 확인 ps aux | grep postgres # 포트 확인 lsof -i :5432 # 둜그 확인 tail -f /usr/local/var/log/postgres.log ``` ### κΆŒν•œ 였λ₯˜ ```sql -- λ°μ΄ν„°λ² μ΄μŠ€ μ†Œμœ μž λ³€κ²½ ALTER DATABASE kumamoto_travel OWNER TO kumamoto_user; -- ν…Œμ΄λΈ” κΆŒν•œ λΆ€μ—¬ GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO kumamoto_user; ``` ### μŠ€ν‚€λ§ˆ μ΄ˆκΈ°ν™” ```bash # λͺ¨λ“  ν…Œμ΄λΈ” μ‚­μ œ ν›„ μž¬μƒμ„± psql kumamoto_travel -c "DROP SCHEMA public CASCADE; CREATE SCHEMA public;" psql kumamoto_travel < server/schema_v2.sql ``` ## πŸ“ˆ μ„±λŠ₯ μ΅œμ ν™” ### 인덱슀 확인 ```sql -- 인덱슀 μ‚¬μš© ν˜„ν™© SELECT schemaname, tablename, indexname, idx_tup_read, idx_tup_fetch FROM pg_stat_user_indexes; -- 느린 쿼리 확인 SELECT query, mean_time, calls FROM pg_stat_statements ORDER BY mean_time DESC; ``` ### μ—°κ²° ν’€ μ„€μ • ```javascript // server/db.jsμ—μ„œ μ—°κ²° ν’€ μ‘°μ • const pool = new Pool({ connectionString: process.env.DATABASE_URL, max: 20, // μ΅œλŒ€ μ—°κ²° 수 idleTimeoutMillis: 30000, connectionTimeoutMillis: 2000, }); ``` ## πŸš€ ν”„λ‘œλ•μ…˜ 배포 ### ν™˜κ²½ λ³€μˆ˜ (ν”„λ‘œλ•μ…˜) ```env DATABASE_URL=postgresql://user:password@host:port/database?sslmode=require JWT_SECRET=production-secret-key-very-long-and-random NODE_ENV=production PORT=3000 ``` ### SSL μ„€μ • ```javascript // SSL μ—°κ²° (ν”„λ‘œλ•μ…˜) const pool = new Pool({ connectionString: process.env.DATABASE_URL, ssl: process.env.NODE_ENV === 'production' ? { rejectUnauthorized: false } : false }); ``` ## πŸ“ž 지원 λ¬Έμ œκ°€ λ°œμƒν•˜λ©΄ λ‹€μŒμ„ ν™•μΈν•˜μ„Έμš”: 1. PostgreSQL μ„œλΉ„μŠ€ μ‹€ν–‰ μƒνƒœ 2. .env 파일의 DATABASE_URL μ •ν™•μ„± 3. λ°μ΄ν„°λ² μ΄μŠ€ μ‚¬μš©μž κΆŒν•œ 4. λ°©ν™”λ²½ μ„€μ • (포트 5432, 3000) μ„±κ³΅μ μœΌλ‘œ μ„€μ •λ˜λ©΄ λΈŒλΌμš°μ €μ—μ„œ `http://localhost:5173`에 μ ‘μ†ν•˜μ—¬ 초기 섀정을 μ™„λ£Œν•  수 μžˆμŠ΅λ‹ˆλ‹€.