Prerequisites
- PostgreSQL 13 or later
- Database user with CREATE privileges
- Network access to PostgreSQL server
PostgreSQL Installation
- Ubuntu/Debian
- CentOS/RHEL
- macOS
- Docker
Database Configuration
Configure PostgreSQL authentication
Edit Add or modify this line:Reload PostgreSQL:
pg_hba.conf to allow password authentication:Database Schema
Faculty Bot uses the following database schema:Tables Overview
verified_users - User Verification Records
verified_users - User Verification Records
Stores verified user information and their associated email addresses.Columns:
user_id: Discord user ID (Primary Key)user_email: Verified student email address
user_xp - Experience and Leveling System
user_xp - Experience and Leveling System
Tracks user experience points and levels.Columns:
user_id: Discord user ID (Primary Key)user_xp: Total experience points (floating point)user_level: Current user level (integer)
voice_channels - Temporary Voice Channel Management
voice_channels - Temporary Voice Channel Management
Manages dynamically created voice channels.Columns:
channel_id: Discord channel ID (Primary Key)owner_id: Discord user ID of channel creatordeletion_marker: Flag for channels pending deletion
mensaplan - Meal Plan Tracking
mensaplan - Meal Plan Tracking
Tracks posted meal plans to prevent duplicates.Columns:
date: Date of meal plan (Primary Key)posted: Whether the plan has been postedplan_hash: Hash of the meal plan PDF for change detection
ads - Advertisement Management
ads - Advertisement Management
Tracks advertisements for automatic deletion.Columns:
message_id: Discord message ID (Primary Key)posted_at: Timestamp when ad was posted
rules - Server Rules Storage
rules - Server Rules Storage
Stores server rules with versioning.Columns:
rule_number: Rule identifier (Primary Key)rule_text: Content of the rule
semestermods - Semester Moderators
semestermods - Semester Moderators
Tracks users with semester moderator permissions.Columns:
user_id: Discord user ID (Primary Key)
posted_rss - RSS Feed Tracking
posted_rss - RSS Feed Tracking
Prevents duplicate RSS feed posts.Columns:
message_id: Discord message ID (Primary Key)rss_title: Title of the RSS itemchannel_id: Channel where item was posted
Running Migrations
Locate migration file
The migration script is located at
migrations/faculty_manager.sql in the repository.Connection Configuration
Configure the database connection in your.env file:
The connection string format is:
postgres://USER:PASSWORD@HOST:PORT/DATABASEDocker Compose Database Setup
For automated database initialization with Docker:The migration script in
/docker-entrypoint-initdb.d/ runs automatically on first container startup.Database Maintenance
Backup Database
Restore Database
Vacuum and Analyze
Optimize database performance:Monitoring and Performance
Check Database Size
Check Table Sizes
Monitor Active Connections
View Slow Queries
Enable query logging inpostgresql.conf:
Troubleshooting
Connection refused errors
Connection refused errors
Symptoms:
could not connect to server: Connection refusedSolutions:- Check if PostgreSQL is running:
sudo systemctl status postgresql - Verify PostgreSQL is listening on port 5432:
sudo netstat -plnt | grep 5432 - Check
postgresql.confforlisten_addresses: - Restart PostgreSQL:
sudo systemctl restart postgresql
Authentication failed errors
Authentication failed errors
Symptoms:
FATAL: password authentication failed for userSolutions:- Verify username and password in
DATABASE_URL - Check
pg_hba.confhas correct authentication method (md5 or scram-sha-256) - Test connection manually:
psql -U faculty_manager -d faculty_manager -h localhost - Reset password if needed:
Permission denied on table operations
Permission denied on table operations
Symptoms:
ERROR: permission denied for tableSolutions:- Grant necessary permissions:
- Verify table ownership:
Database disk space issues
Database disk space issues
Symptoms:
ERROR: could not extend file ... No space left on deviceSolutions:- Check disk usage:
df -h - Clean up old data:
- Run VACUUM to reclaim space:
Migration script fails
Migration script fails
Symptoms: Errors when running
faculty_manager.sqlSolutions:- Check if tables already exist:
- Drop and recreate database if needed:
- Verify SQL syntax in migration file
- Check PostgreSQL logs for detailed error messages
Too many open connections
Too many open connections
Symptoms:
FATAL: sorry, too many clients alreadySolutions:- Check current connections:
- Increase
max_connectionsinpostgresql.conf: - Restart PostgreSQL:
sudo systemctl restart postgresql - Implement connection pooling (PgBouncer)
Security Best Practices
- Use strong, unique passwords for database users
- Restrict network access using
pg_hba.conf - Enable SSL/TLS for remote connections
- Regularly update PostgreSQL to latest patch version
- Implement automated backups with off-site storage
- Use separate database users for different applications
- Enable audit logging for sensitive operations
- Set up monitoring and alerting for suspicious activity
Enable SSL Connections
Inpostgresql.conf:
pg_hba.conf:
DATABASE_URL:
Production Recommendations
Automated Backups
Set up daily automated backups with cron:
Monitoring
Use tools like:
- pgAdmin for GUI management
- pg_stat_statements for query analysis
- Prometheus + postgres_exporter for metrics
Connection Pooling
Use PgBouncer to manage connection pooling:Update
DATABASE_URL to point to PgBouncer portReplication
Set up streaming replication for high availability:
- Configure primary and standby servers
- Enable WAL archiving
- Set up automatic failover
Next Steps
Docker Deployment
Deploy with Docker for simplified setup
Manual Deployment
Deploy manually with process managers