Skip to content

Use SQL queries to answer critical SaaS growth questions with PostgreSQL. Realistic schema for a mock project management app (Free/Basic/Premium plans), 1000+ sample users, 50+ analytics queries for MRR, churn, conversion, retention & more. One-command setup! πŸ“Š

License

Notifications You must be signed in to change notification settings

dannyblaker/saas-analytics-postgres

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“Š SaaS Analytics with PostgreSQL

A comprehensive demonstration of how PostgreSQL and SQL queries can answer critical growth and marketing questions for SaaS businesses

This repository demonstrates the power of PostgreSQL for SaaS analytics by providing a realistic database schema, sample data, and comprehensive SQL queries that answer key growth questions for a project management SaaS application.

Run the entire project with a single command: docker compose up

The SQL queries included in this project are inspired by a large scale real-world project I developed for an enterprise client.

You are most welcome to use this code in your commercial projects, all that I ask in return is that you credit my work by providing a link back to this repository. Thank you & Enjoy!

✨ Key Features

  • 🐳 One-Command Setup - docker compose up gets everything running
  • πŸ“Š Interactive Jupyter Notebook - Visual analytics with Plotly charts
  • πŸ—„οΈ Realistic Database - 1000+ users, 12 months of behavioral data
  • πŸ’° 50+ SQL Queries - All critical SaaS metrics covered
  • πŸ“ˆ Beautiful Visualizations - MRR trends, cohort retention, conversion funnels
  • 🎯 Production-Ready Schema - Optimized indexes and relationships
  • 🌍 Multi-dimensional Analysis - Channel, geographic, and plan-based insights

πŸš€ Quick Start

One-Command Setup with Docker 🐳

The easiest way to get started:

git clone <your-repo-url>
cd postgres_demo
docker compose up

That's it! This single command will:

  • βœ… Start PostgreSQL with sample data (1000+ users, 12 months history)
  • βœ… Launch Jupyter notebook with interactive analytics
  • βœ… Set up all necessary Python libraries for visualization
  • βœ… Make everything accessible via your browser

Access the services:

  • πŸ“Š Jupyter Notebook: http://localhost:8888 (Interactive analytics dashboard)
  • πŸ—„οΈ PostgreSQL: localhost:5432 (Direct database access)
  • πŸ”§ pgAdmin (optional): docker compose --profile pgadmin up β†’ http://localhost:8080

Prerequisites

  • Docker and Docker Compose installed
  • 2GB free disk space
  • Web browser

Alternative: Manual Setup

If you prefer to install PostgreSQL locally:

./setup_database.sh

This script will:

  • βœ… Create a PostgreSQL database with sample data
  • βœ… Set up realistic user journeys and subscription patterns
  • βœ… Generate schema documentation and diagrams
  • βœ… Verify the installation with key metrics

Manual Setup

If you prefer manual setup:

  1. Create the database:

    CREATE DATABASE saas_analytics_demo;
    CREATE USER saas_user WITH PASSWORD 'demo_password';
    GRANT ALL PRIVILEGES ON DATABASE saas_analytics_demo TO saas_user;
  2. Load the schema:

    psql -U saas_user -d saas_analytics_demo -f schema.sql
  3. Import sample data:

    psql -U saas_user -d saas_analytics_demo -f sample_data.sql
  4. Generate schema diagram:

    python3 generate_erd.py

πŸ—οΈ Database Schema

Our schema models a realistic SaaS project management application with three subscription tiers:

Core Entities

  • πŸ‘₯ Users - Customer accounts with signup attribution
  • πŸ’³ Subscriptions - Plan history and billing information
  • πŸ“‹ Projects - User-created projects (core product value)
  • βœ… Tasks - Project tasks (engagement indicator)
  • πŸ‘« Team Memberships - Collaboration features
  • πŸ’° Revenue Events - All financial transactions
  • πŸ“Š Analytics Events - User behavior tracking

Subscription Plans

  • πŸ†“ Free - $0/month (3 projects, 1 team member)
  • ⭐ Basic - $9.99/month (10 projects, 5 team members)
  • πŸš€ Premium - $19.99/month (unlimited projects and team members)

Database Schema

πŸ“Š Interactive Jupyter Notebook

NEW! All analytics queries are now available in an interactive, visual Jupyter notebook!

After running docker compose up, open your browser to http://localhost:8888 to access:

  • πŸ“Š Interactive visualizations with Plotly and Matplotlib
  • πŸ” Step-by-step analysis of all key SaaS metrics
  • πŸ“ˆ Beautiful charts for MRR, churn, conversion, and retention
  • 🎯 Complete conversion funnels with visual flow
  • 🌍 Geographic and channel performance analysis
  • πŸ’‘ Customer Lifetime Value (LTV) calculations

The notebook (notebooks/SaaS_Analytics_Demo.ipynb) covers:

  1. Key Metrics Overview Dashboard
  2. Revenue & MRR Analytics
  3. Conversion & Upgrade Analysis
  4. Retention & Churn Tracking
  5. Engagement & Activation Metrics
  6. Marketing Channel Performance
  7. Customer Lifetime Value
  8. Complete Conversion Funnel

πŸ“ˆ Analytics Capabilities

Revenue Analytics

-- Monthly Recurring Revenue by Plan
SELECT 
    p.name as plan,
    SUM(CASE 
        WHEN s.billing_cycle = 'monthly' THEN s.mrr
        WHEN s.billing_cycle = 'annual' THEN s.mrr / 12
    END) as monthly_recurring_revenue
FROM subscriptions s
JOIN plans p ON s.plan_id = p.id
WHERE s.status = 'active'
GROUP BY p.name;

Conversion Funnel

-- Free to Paid Conversion Analysis
WITH conversion_funnel AS (
    SELECT 
        COUNT(DISTINCT u.id) as total_signups,
        COUNT(DISTINCT CASE WHEN s.plan_id != 1 THEN u.id END) as paid_conversions
    FROM users u
    LEFT JOIN subscriptions s ON u.id = s.user_id AND s.status = 'active'
)
SELECT 
    total_signups,
    paid_conversions,
    ROUND(paid_conversions * 100.0 / total_signups, 2) as conversion_rate_pct
FROM conversion_funnel;

Retention Analysis

-- Cohort Retention by Plan
SELECT 
    DATE_TRUNC('month', u.created_at) as cohort_month,
    p.name as plan,
    COUNT(DISTINCT u.id) as cohort_size,
    COUNT(DISTINCT CASE 
        WHEN u.last_login_at >= DATE_TRUNC('month', u.created_at) + INTERVAL '1 month' 
        THEN u.id 
    END) * 100.0 / COUNT(DISTINCT u.id) as retention_1_month_pct
FROM users u
JOIN subscriptions s ON u.id = s.user_id
JOIN plans p ON s.plan_id = p.id
WHERE s.status = 'active'
GROUP BY DATE_TRUNC('month', u.created_at), p.name
ORDER BY cohort_month DESC;

πŸ“Š Key Metrics Dashboard

Run these queries in analytics_queries.sql to get instant insights:

Metric Description
πŸ’° MRR Growth Monthly recurring revenue trends
πŸ”„ Conversion Rates Free-to-paid conversion analysis
πŸ“‰ Churn Analysis Customer retention and churn patterns
πŸ‘€ ARPU by Plan Average revenue per user segmentation
🎯 Activation Funnel User onboarding and activation rates
🌍 Geographic Performance Revenue and conversion by country
πŸ“± Channel Attribution Marketing channel effectiveness

🎯 Sample Analytics Questions Answered

This database schema and query collection can answer 50+ critical SaaS questions, including:

πŸ’Έ Revenue Questions

  • What's our MRR growth rate by plan?
  • Which plan contributes most to revenue?
  • What's the average LTV by customer segment?

πŸ”„ Conversion Questions

  • What percentage of free users upgrade to paid?
  • How long does it take users to upgrade?
  • Which features drive plan upgrades?

πŸ“Š Retention Questions

  • How does churn vary by plan?
  • What's our cohort retention at 30/60/90 days?
  • Which user behaviors predict churn?

πŸš€ Growth Questions

  • Which acquisition channels bring the best customers?
  • Where are the biggest funnel drop-offs?
  • How do engagement patterns differ by plan?

See key_questions.md for the complete list of analytics questions.

πŸ“ File Structure

postgres_demo/
β”œβ”€β”€ πŸ“„ README.md                 # This file
β”œβ”€β”€ πŸ“Š key_questions.md          # Comprehensive list of analytics questions
β”œβ”€β”€ πŸ—„οΈ schema.sql               # Complete PostgreSQL schema
β”œβ”€β”€ πŸ“‹ sample_data.sql           # Realistic sample data generation
β”œβ”€β”€ πŸ” analytics_queries.sql     # 50+ analytical SQL queries
β”œβ”€β”€ πŸ–ΌοΈ generate_erd.py          # Schema diagram generator
β”œβ”€β”€ βš™οΈ setup_database.sh         # One-command setup script
β”œβ”€β”€ πŸ”— docker-compose.yml        # Docker setup (optional)
└── πŸ“ˆ schema_diagram.png        # Visual database schema

🐳 Docker Alternative

For a completely isolated setup using Docker:

# Start PostgreSQL with Docker
docker-compose up -d

# Run setup (connects to Docker instance)
./setup_database.sh

πŸ”§ Customization

Adding New Metrics

  1. Extend the schema in schema.sql with new tables or columns
  2. Update sample data in sample_data.sql to populate new fields
  3. Write queries in analytics_queries.sql to analyze the new data
  4. Regenerate ERD with python3 generate_erd.py

Modifying Plans

Edit the plans table in schema.sql:

INSERT INTO plans (name, price_monthly, price_annual, max_projects, max_team_members, features) VALUES
('enterprise', 49.99, 499.90, -1, -1, '{"priority_support": true, "sso": true, "custom_integrations": true}');

Real-World Integration

  • Replace sample data with actual customer data
  • Connect to your existing user authentication system
  • Integrate with payment processors (Stripe, PayPal, etc.)
  • Add ETL processes for ongoing data updates

πŸ“š Learn More

Recommended Reading

Advanced Topics

  • Time-series analysis for trend detection
  • Cohort analysis for retention insights
  • Predictive modeling for churn prevention
  • Real-time dashboards with materialized views
  • Data warehousing patterns for analytics at scale

🀝 Contributing

We welcome contributions! Here's how you can help:

  1. πŸ› Report issues - Found a bug or have a suggestion?
  2. πŸ“ Improve queries - More efficient SQL or additional analytics
  3. πŸ“Š Add metrics - New KPIs or industry-specific measurements
  4. πŸ”§ Enhance tooling - Better setup scripts or visualization tools

Development Setup

git fork <repo-url>
git clone <your-fork>
cd postgres_demo

# Make your changes
# Test thoroughly
git commit -m "Add new churn prediction query"
git push origin feature/churn-prediction

# Open a pull request

⭐ Kindly star this repository if it helped you understand SaaS analytics with PostgreSQL, Thanks & much appreciated, Warm regards, Danny.

About

Use SQL queries to answer critical SaaS growth questions with PostgreSQL. Realistic schema for a mock project management app (Free/Basic/Premium plans), 1000+ sample users, 50+ analytics queries for MRR, churn, conversion, retention & more. One-command setup! πŸ“Š

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages