2311061234/DATABASE_CONNECTION.md
2025-12-22 05:07:33 +08:00

89 lines
2.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# VibeVault 数据库连接配置
## 远程数据库连接信息
### 生产环境配置
- **数据库类型**: PostgreSQL
- **主机地址**: 49.234.193.192
- **端口**: 5432
- **数据库名**: vibevault
- **用户名**: postgres
- **密码**: postgres
### 配置文件说明
#### 1. 开发环境 (application.properties)
默认使用H2内存数据库适合本地开发和测试
```properties
# 开发/测试时使用 H2 内存数据库
spring.datasource.url=jdbc:h2:mem:vibevault;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
```
#### 2. 生产环境 (application-prod.properties)
使用远程PostgreSQL数据库
```properties
# 生产环境使用 PostgreSQL 远程数据库
spring.datasource.url=jdbc:postgresql://49.234.193.192:5432/vibevault
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.driver-class-name=org.postgresql.Driver
```
### 环境切换
#### 方式1: 启动参数
```bash
# 开发环境 (默认)
./gradlew bootRun
# 生产环境
./gradlew bootRun --args='--spring.profiles.active=prod'
```
#### 方式2: 环境变量
```bash
# 设置环境变量
set SPRING_PROFILES_ACTIVE=prod
./gradlew bootRun
```
#### 方式3: 系统属性
```bash
# 通过系统属性
./gradlew bootRun -Dspring.profiles.active=prod
```
### 数据库连接测试
项目包含数据库连接测试类 `DatabaseConnectionTest`,可以验证与远程数据库的连接:
```bash
# 运行数据库连接测试
./gradlew test --tests DatabaseConnectionTest
```
### 注意事项
1. **网络连接**: 确保可以访问远程数据库服务器 `49.234.193.192:5432`
2. **防火墙**: 检查本地防火墙设置确保5432端口未被阻止
3. **数据库权限**: 确保数据库用户具有必要的读写权限
4. **连接池**: Spring Boot会自动配置Hikari连接池
### 故障排除
如果连接失败,请检查:
- 网络连接是否正常
- 数据库服务是否正在运行
- 用户名和密码是否正确
- 数据库是否存在
### 支持的数据库操作
- ✅ 自动建表 (spring.jpa.hibernate.ddl-auto=update)
- ✅ 事务管理
- ✅ 连接池
- ✅ JPA/Hibernate支持
- ✅ 数据库迁移 (如果需要)