This guide provides step-by-step instructions for setting up Postgres for the Django migration, both locally and on Heroku.
# 1. Install Postgres
brew install postgresql@15
# 2. Start Postgres service
brew services start postgresql@15
# 3. Verify installation
psql --version
# 4. Create database
createdb pythoness_db
# 5. Create user (optional - you can use your system user)
createuser pythoness_user -P
# When prompted, enter a password
# 6. Grant permissions
psql pythoness_dbIn the psql prompt:
GRANT ALL PRIVILEGES ON DATABASE pythoness_db TO pythoness_user;
\q- Download from postgresapp.com
- Install and launch the app
- Click "Initialize" to create a new server
- The app will create a default database with your username
- Use the default connection settings in Django
# 1. Update package list
sudo apt-get update
# 2. Install Postgres
sudo apt-get install postgresql postgresql-contrib
# 3. Start Postgres service
sudo systemctl start postgresql
sudo systemctl enable postgresql
# 4. Switch to postgres user
sudo -u postgres psqlIn the psql prompt:
-- Create database
CREATE DATABASE pythoness_db;
-- Create user
CREATE USER pythoness_user WITH PASSWORD 'your_secure_password';
-- Grant privileges
GRANT ALL PRIVILEGES ON DATABASE pythoness_db TO pythoness_user;
-- Exit
\q-
Download Postgres:
- Visit postgresql.org/download/windows
- Download the installer
- Run the installer and follow the setup wizard
- Remember the password you set for the
postgresuser
-
Using pgAdmin (GUI):
- Open pgAdmin (installed with Postgres)
- Right-click "Databases" → "Create" → "Database"
- Name:
pythoness_db - Right-click "Login/Group Roles" → "Create" → "Login/Group Role"
- Name:
pythoness_user, set password - Go to "Privileges" tab and grant all permissions
-
Using Command Line (psql):
-- Connect as postgres user psql -U postgres -- Create database CREATE DATABASE pythoness_db; -- Create user CREATE USER pythoness_user WITH PASSWORD 'your_password'; -- Grant privileges GRANT ALL PRIVILEGES ON DATABASE pythoness_db TO pythoness_user;
Add to requirements.txt:
psycopg2-binary>=2.9.9
dj-database-url>=2.1.0
python-decouple>=3.8Install:
pip install psycopg2-binary dj-database-url python-decouple# settings/base.py
import dj_database_url
from decouple import config
import os
# Database configuration
# For local development, you can use a .env file
# For production, Heroku sets DATABASE_URL automatically
DATABASES = {
'default': dj_database_url.config(
default=config(
'DATABASE_URL',
default=f"postgresql://pythoness_user:your_password@localhost:5432/pythoness_db"
),
conn_max_age=600,
conn_health_checks=True,
)
}Create a .env file in your project root (add to .gitignore):
# .env
DATABASE_URL=postgresql://pythoness_user:your_password@localhost:5432/pythoness_db
SECRET_KEY=your-secret-key-here
DEBUG=True# Create migrations
python manage.py makemigrations
# Apply migrations
python manage.py migrate
# Create superuser (for Django admin)
python manage.py createsuperuser# Add Postgres (free tier for testing)
heroku addons:create heroku-postgresql:mini
# For production, use a paid tier:
# heroku addons:create heroku-postgresql:standard-0Heroku automatically sets the DATABASE_URL environment variable:
# Check the DATABASE_URL
heroku config:get DATABASE_URL
# View all config vars
heroku config# Run migrations
heroku run python manage.py migrate
# Create superuser
heroku run python manage.py createsuperuser# Connect to Heroku Postgres via psql
heroku pg:psql
# Or get connection string for external tools
heroku pg:credentials:url# Connect to database
psql pythoness_db
# Or with user
psql -U pythoness_user -d pythoness_db-- List all databases
\l
-- Connect to a database
\c pythoness_db
-- List all tables
\dt
-- Describe a table structure
\d table_name
-- List all users
\du
-- Show current database
SELECT current_database();
-- Exit psql
\q# Create migrations
python manage.py makemigrations
# Apply migrations
python manage.py migrate
# Show migration status
python manage.py showmigrations
# Rollback last migration
python manage.py migrate app_name previous_migration_number
# Create superuser
python manage.py createsuperuser
# Open Django shell with database access
python manage.py shell# View database info
heroku pg:info
# View database size
heroku pg:info --app your-app-name
# Create database backup
heroku pg:backups:capture
# Download backup
heroku pg:backups:download
# Restore from backup
heroku pg:backups:restore BACKUP_URL DATABASE_URL
# Reset database (⚠️ DESTRUCTIVE)
heroku pg:reset DATABASE_URLError: "could not connect to server"
- Check if Postgres is running:
brew services list(macOS) orsudo systemctl status postgresql(Linux) - Verify connection string format:
postgresql://user:password@host:port/database - Check firewall settings
Error: "password authentication failed"
- Verify username and password
- Check
pg_hba.conffile for authentication settings - Try resetting password:
ALTER USER pythoness_user WITH PASSWORD 'new_password';
Error: "database does not exist"
- Create the database:
createdb pythoness_db - Or create via psql:
CREATE DATABASE pythoness_db;
Error: "relation does not exist"
- Run migrations:
python manage.py migrate - Check if app is in
INSTALLED_APPS
Error: "no such table"
- Run migrations:
python manage.py migrate - Check migration files exist in
app/migrations/
Error: "DATABASE_URL not set"
- Add Postgres addon:
heroku addons:create heroku-postgresql:mini - Verify:
heroku config:get DATABASE_URL
Error: "Connection timeout"
- Check Heroku app status:
heroku ps - Verify Postgres addon is active:
heroku addons
postgresql://username:password@localhost:5432/database_name
postgres://[user]:[password]@[host]:[port]/[database]
(Set automatically by Heroku Postgres addon)
# Local Development
DATABASE_URL=postgresql://pythoness_user:mypassword@localhost:5432/pythoness_db
# Production (Heroku sets this automatically)
# DATABASE_URL is set by Heroku Postgres addon- ✅ Postgres installed and running locally
- ✅ Database and user created
- ✅ Django settings configured
- ✅ Migrations run successfully
- ✅ Superuser created
- ✅ Heroku Postgres addon added (when ready to deploy)
- Postgres Documentation
- Django Database Documentation
- dj-database-url Documentation
- Heroku Postgres Documentation
Need Help? If you run into issues, check:
- Postgres service is running
- Connection string format is correct
- User has proper permissions
- Database exists
- Django settings are configured correctly