Refactor database configuration and schema for Crypto Trading Bot Platform
- Updated `docker-compose.yml` to remove hardcoded passwords, relying on environment variables for PostgreSQL and Redis configurations. - Modified `env.template` to reflect new password settings and ensure secure handling of sensitive information. - Introduced a new `database/connection.py` file for improved database connection management, including connection pooling and session handling. - Updated `database/models.py` to align with the new schema in `schema_clean.sql`, utilizing JSONB for optimized data storage. - Enhanced `setup.md` documentation to clarify the initialization process and emphasize the importance of the `.env` file for configuration. - Added a new `scripts/init_database.py` script for automated database initialization and verification, ensuring all tables are created as expected.
This commit is contained in:
@@ -53,19 +53,23 @@ Copy-Item env.template .env
|
||||
cp env.template .env
|
||||
```
|
||||
|
||||
**Important**: The `.env` file contains pre-configured secure passwords. **Do not commit this file to version control.**
|
||||
|
||||
### 3. Configure Custom Ports (Optional)
|
||||
|
||||
If you have other PostgreSQL instances running, the default configuration uses port `5434` to avoid conflicts.
|
||||
**Important**:
|
||||
- The `.env` file is **REQUIRED** - the application will not work without it
|
||||
- The `.env` file contains secure passwords for database and Redis
|
||||
- **Never commit the `.env` file to version control**
|
||||
- All credentials must be loaded from environment variables - no hardcoded passwords exist in the codebase
|
||||
|
||||
Current configuration in `.env`:
|
||||
```env
|
||||
POSTGRES_PORT=5434
|
||||
POSTGRES_PASSWORD=sdkjfh534^jh
|
||||
REDIS_PASSWORD=redis987secure
|
||||
POSTGRES_PASSWORD=your_secure_password_here
|
||||
REDIS_PASSWORD=your_redis_password_here
|
||||
```
|
||||
|
||||
### 3. Configure Custom Ports (Optional)
|
||||
|
||||
If you have other PostgreSQL instances running, the default configuration uses port `5434` to avoid conflicts. You can modify these in your `.env` file.
|
||||
|
||||
## Database Setup
|
||||
|
||||
### 1. Start Database Services
|
||||
@@ -81,7 +85,7 @@ This will:
|
||||
- Create a Redis instance on port `6379`
|
||||
- Set up persistent volumes for data storage
|
||||
- Configure password authentication
|
||||
- **Automatically initialize the database schema** using scripts in `database/init/`
|
||||
- **Automatically initialize the database schema** using the clean schema (without TimescaleDB hypertables for simpler setup)
|
||||
|
||||
### 2. Verify Services are Running
|
||||
|
||||
@@ -99,42 +103,28 @@ dashboard_redis redis:7-alpine "docker-entrypoint.s…
|
||||
|
||||
### 3. Verify Database Schema
|
||||
|
||||
Check if tables were created successfully:
|
||||
The database schema is automatically initialized when containers start. You can verify it worked:
|
||||
|
||||
```powershell
|
||||
docker exec dashboard_postgres psql -U dashboard -d dashboard -c "\dt"
|
||||
```
|
||||
|
||||
Expected output should show tables: `bots`, `bot_performance`, `market_data`, `signals`, `supported_exchanges`, `supported_timeframes`, `trades`
|
||||
Expected output should show tables: `bots`, `bot_performance`, `market_data`, `raw_trades`, `signals`, `supported_exchanges`, `supported_timeframes`, `trades`
|
||||
|
||||
### 4. Manual Schema Application (If Needed)
|
||||
### 4. Test Database Initialization Script (Optional)
|
||||
|
||||
If the automatic initialization didn't work, you can manually apply the schema:
|
||||
You can also test the database initialization using the Python script:
|
||||
|
||||
```powershell
|
||||
# Apply the complete schema
|
||||
Get-Content database/schema.sql | docker exec -i dashboard_postgres psql -U dashboard -d dashboard
|
||||
|
||||
# Or apply the clean version (without TimescaleDB hypertables)
|
||||
Get-Content database/schema_clean.sql | docker exec -i dashboard_postgres psql -U dashboard -d dashboard
|
||||
uv run .\scripts\init_database.py
|
||||
```
|
||||
|
||||
### 5. Test Database Connections
|
||||
|
||||
Test PostgreSQL connection:
|
||||
```powershell
|
||||
# Test port accessibility
|
||||
Test-NetConnection -ComputerName localhost -Port 5434
|
||||
|
||||
# Test database connection and check schema
|
||||
docker exec dashboard_postgres psql -U dashboard -d dashboard -c "SELECT COUNT(*) FROM bots;"
|
||||
```
|
||||
|
||||
Test Redis connection:
|
||||
```powershell
|
||||
docker exec dashboard_redis redis-cli -a redis987secure ping
|
||||
```
|
||||
|
||||
Expected output: `PONG`
|
||||
This script will:
|
||||
- Load environment variables from `.env` file
|
||||
- Test database connection
|
||||
- Create all tables using SQLAlchemy models
|
||||
- Verify all expected tables exist
|
||||
- Show connection pool status
|
||||
|
||||
## Application Setup
|
||||
|
||||
@@ -200,12 +190,12 @@ POSTGRES_HOST=localhost
|
||||
POSTGRES_PORT=5434
|
||||
POSTGRES_DB=dashboard
|
||||
POSTGRES_USER=dashboard
|
||||
POSTGRES_PASSWORD=sdkjfh534^jh
|
||||
POSTGRES_PASSWORD=your_secure_password_here
|
||||
|
||||
# Redis Configuration
|
||||
REDIS_HOST=localhost
|
||||
REDIS_PORT=6379
|
||||
REDIS_PASSWORD=redis987secure
|
||||
REDIS_PASSWORD=your_redis_password_here
|
||||
|
||||
# Application Configuration
|
||||
DASH_HOST=0.0.0.0
|
||||
@@ -356,24 +346,14 @@ uv run python test_connection.py
|
||||
# Check initialization logs
|
||||
docker-compose logs postgres
|
||||
|
||||
# Manually apply schema if needed
|
||||
Get-Content database/schema_clean.sql | docker exec -i dashboard_postgres psql -U dashboard -d dashboard
|
||||
# Use the Python initialization script to create/verify schema
|
||||
uv run .\scripts\init_database.py
|
||||
|
||||
# Verify tables were created
|
||||
docker exec dashboard_postgres psql -U dashboard -d dashboard -c "\dt"
|
||||
```
|
||||
|
||||
#### 5. TimescaleDB Extension Issues
|
||||
|
||||
**Error**: `extension "timescaledb" is not available`
|
||||
|
||||
**Solution**:
|
||||
- Ensure using TimescaleDB image: `timescale/timescaledb:latest-pg15`
|
||||
- Check docker-compose.yml has correct image
|
||||
- Restart containers: `docker-compose down && docker-compose up -d`
|
||||
- Use clean schema if needed: `database/schema_clean.sql`
|
||||
|
||||
#### 6. Python Dependencies Issues
|
||||
#### 5. Application Dependencies Issues
|
||||
|
||||
**Error**: Package installation failures
|
||||
|
||||
@@ -425,7 +405,7 @@ docker exec -it dashboard_postgres psql -U dashboard -d dashboard
|
||||
#### Access Redis CLI
|
||||
|
||||
```powershell
|
||||
docker exec -it dashboard_redis redis-cli -a redis987secure
|
||||
docker exec -it dashboard_redis redis-cli -a $env:REDIS_PASSWORD
|
||||
```
|
||||
|
||||
## Security Notes
|
||||
|
||||
Reference in New Issue
Block a user