This guide provides instructions for backing up (exporting), restoring (importing), and synchronizing the PostgreSQL database for the Stévillis Learning project.
- PostgreSQL Client Tools: Ensure
psql,pg_dump,createdb, anddropdbare installed and available in your system'sPATH. - Python Environment: Activated virtual environment (
.venv) with Django installed. - Connection Details: Access to Host, Port, User, Password, and Database Name (
stevillearning).
To backup the production database from Railway:
PowerShell
# Syntax: pg_dump postgresql://<USER>:<PASSWORD>@<HOST>:<PORT>/<DB_NAME> -f <OUTPUT_FILE>
# Example:
pg_dump postgresql://postgres:PGPASSWORD@monorail.proxy.rlwy.net:51892/stevillearning -f backup-28-06-2025.sqlTo backup the database from Supabase:
PowerShell
# Syntax: $env:PGPASSWORD="<PASSWORD>"; pg_dump -U <USER> -h <HOST> -p <PORT> <DB_NAME> -f <OUTPUT_FILE>
# Example:
$env:PGPASSWORD="your_password"; pg_dump -U postgres -h aws-0-sa-east-1.pooler.supabase.com -p 6543 postgres -f backup-supabase.sqlTo backup your local database:
PowerShell
# Syntax: pg_dump -U <USER> -h localhost -p <PORT> <DB_NAME> -f <OUTPUT_FILE>
# Example:
pg_dump -U postgres -h localhost -p 5432 stevillearning -f backup-local.sqlTo import a backup file into your local database:
PowerShell / CMD
# Syntax: psql -U <USER> -h localhost -p <PORT> -d <DB_NAME> -f <INPUT_FILE>
# Example:
$env:PGPASSWORD="postgres"; psql -U postgres -h localhost -p 5432 -d stevillearning -f backup-28-06-2025.sqlTo import a backup file into Supabase:
PowerShell
# Syntax: psql -h <HOST> -U <USER> -p <PORT> -d <DB_NAME> -f <INPUT_FILE>
# Example:
psql -h aws-0-sa-east-1.pooler.supabase.com -U postgres.asdfghjklçpoiuytrewq -p 5432 -d postgres -f backup-local.sqlAfter importing a database backup into local or remote PostgreSQL, Django may report unapplied migrations (e.g., You have 21 unapplied migration(s)...) because the django_migrations table records were not populated or were skipped during restore.
If your database tables (course, institution, certification, etc.) are already populated from the SQL dump:
PowerShell
# Mark all app migrations as applied without executing DDL statements:
python manage.py migrate --fake
# Or specifically for learning_hub:
python manage.py migrate learning_hub --fake- Cause: Django checks the
django_migrationstable against physical migration files inlearning_hub/migrations/. If the SQL dump did not populatedjango_migrations, Django considers all migrations pending. - Solution: Run
python manage.py migrate --fakeas documented in Section 3.
Sometimes backups created on Windows or transferred between systems may throw ERROR: invalid byte sequence for encoding "UTF8": 0xff.
Solution: Convert the file encoding to UTF-8 using PowerShell before importing:
PowerShell
# Syntax: Get-Content <ORIGINAL_FILE> | Out-File -Encoding utf8 <NEW_FILE>
# Example:
Get-Content backup-28-06-2025.sql | Out-File -Encoding utf8 backup-28-06-2025-utf8.sqlUse backup-28-06-2025-utf8.sql for the import command.
When backing up from services like Railway that include TimescaleDB by default, you may see warnings like pg_dump: warning: there are circular foreign-key constraints on hypertable.
Solution:
This is safe to ignore. To suppress these warnings, exclude internal schemas using --exclude-schema='_timescaledb_*':
PowerShell
pg_dump postgresql://... --exclude-schema='_timescaledb_*' -f backup.sqlIf you encounter FATAL: password authentication failed for user "postgres":
Solution: Pass the password inline using PowerShell environment variables:
PowerShell
$env:PGPASSWORD="your_password"; psql -U postgres -h localhost -p 5432 -d stevillearning -f backup.sqlOr force a password prompt (-W):
PowerShell
psql -U postgres -h localhost -p 5432 -d stevillearning -W -f backup.sqlCause: Importing an SQL dump into a database that already contains tables.
Solution: Clean Import Workflow
-
Drop the existing database:
dropdb -U postgres -h localhost -p 5432 stevillearning
-
Create a fresh, empty database:
createdb -U postgres -h localhost -p 5432 stevillearning
-
Import your backup SQL file:
$env:PGPASSWORD="postgres"; psql -U postgres -h localhost -p 5432 -d stevillearning -f backup-28-06-2025.sql
-
Sync Django migration state:
python manage.py migrate --fake