Connection Setup
Connection String
Set the database URL in your.env file:
.env
Async Driver
TT-Bot automatically converts PostgreSQL URLs to use theasyncpg driver (data/db_utils.py:7):
+asyncpg in your .env.
Database Initialization
The database must be initialized before any operations.Startup Sequence
- Initialize components (
data/database.py:17):
- Create tables:
What Gets Created
The initialization creates three tables:users- User profiles and preferencesvideos- Download historymusic- Audio extraction history
Database Models
TT-Bot uses three SQLAlchemy models defined indata/models/.
Users Table
Stores user information and preferences (data/models/users.py:6):
| Column | Type | Description |
|---|---|---|
user_id | BigInteger | Primary key, Telegram user ID |
registered_at | BigInteger | Unix timestamp of registration |
lang | String | Language code (default: en) |
link | String | User’s referral link |
file_mode | Boolean | Whether user prefers file uploads (default: false) |
ad_count | Integer | Number of ads viewed (default: 0) |
ad_cooldown | BigInteger | Unix timestamp when next ad can be shown |
Videos Table
Tracks all video downloads (data/models/video.py:6):
| Column | Type | Description |
|---|---|---|
pk_id | BigInteger | Primary key, auto-increment |
user_id | BigInteger | Foreign key to users.user_id |
downloaded_at | BigInteger | Unix timestamp of download |
video_link | String | TikTok/Instagram URL |
is_images | Boolean | Whether content is slideshow (default: false) |
is_processed | Boolean | Processing status (default: false) |
is_inline | Boolean | Downloaded via inline mode (default: false) |
Music Table
Tracks audio extractions (data/models/music.py:6):
| Column | Type | Description |
|---|---|---|
pk_id | BigInteger | Primary key, auto-increment |
user_id | BigInteger | Foreign key to users.user_id |
downloaded_at | BigInteger | Unix timestamp of extraction |
video_id | BigInteger | TikTok video ID |
Database Sessions
TT-Bot provides two ways to get database sessions.Dependency Injection (Recommended)
Use with aiogram handlers:Context Manager
Use for standalone operations:Docker Setup
The includeddocker-compose.yml sets up PostgreSQL automatically:
docker-compose.yml
Manual Setup
If not using Docker:1. Install PostgreSQL
2. Create Database
3. Update Environment
.env
4. Install Python Driver
Migrations
TT-Bot currently uses automatic schema creation viaBase.metadata.create_all(). For production:
Repository Pattern
Database operations are abstracted indata/db_service.py. Example operations:
Troubleshooting
Connection Refused
- Verify PostgreSQL is running:
sudo systemctl status postgresql - Check host/port in
DB_URL - Ensure firewall allows port 5432
Authentication Failed
- Verify username/password in
DB_URL - Check PostgreSQL user exists:
psql -U postgres -c "\du"
Database Not Found
Engine Not Initialized
initialize_database_components() before any database operations.
Performance Tuning
Connection Pooling
SQLAlchemy automatically pools connections. For high load, adjust pool size:Indexes
The default schema includes indexes on:users.user_id(primary key)videos.user_id(foreign key)music.user_id(foreign key)

