A simple Flask web application with PostgreSQL database connectivity check and health monitoring.
- ✅ Root route (
/) that displays "Hello, World!" - ✅ Health check endpoint (
/health) with database connectivity status - ✅ PostgreSQL database connection using pg8000 (pure Python adapter)
- ✅ Modern, responsive web interface
- ✅ Ready for deployment on Render.com
- Python 3.13.5 (specified in .python-version file)
- pip (Python package installer)
- PostgreSQL database (optional for local testing)
-
Clone the repository
git clone <your-repo-url> cd py-loan
-
Install dependencies
pip install -r requirements.txt
-
Set up environment variables
Create a
.envfile in the project root:# .env file DATABASE_URL=postgresql://username:password@localhost:5432/database_name FLASK_ENV=developmentOr set environment variables directly:
export DATABASE_URL="postgresql://username:password@localhost:5432/database_name" export FLASK_ENV=development
DATABASE_URL Format:
postgresql://username:password@host:port/database_nameExample:
postgresql://myuser:mypassword@localhost:5432/mydatabase -
Run the application
flask run
The application will be available at
http://localhost:5000
- Visit the homepage: Open
http://localhost:5000in your browser - Check health endpoint: Visit
http://localhost:5000/healthto see the JSON response - Database connectivity: The health endpoint will show database connection status
- GitHub account with your code pushed to a repository
- Render.com account
-
Set up PostgreSQL Database FIRST (Recommended)
Create a PostgreSQL service on Render:
- Go to Render Dashboard
- Click "New +" and select "PostgreSQL"
- Configure the database:
- Name:
your-app-database - Database:
your_app_db - User:
your_app_user - Region: Choose closest to your users
- Name:
- Click "Create Database"
- Important: Copy the connection string from the database dashboard
-
Push your code to GitHub
git add . git commit -m "Initial commit" git push origin main
-
Create a new Web Service on Render
- Go to Render Dashboard
- Click "New +" and select "Web Service"
- Connect your GitHub repository
- Configure the service:
-
Service Configuration
Basic Settings:
- Name:
your-app-name - Environment:
Python 3 - Region: Choose closest to your users
- Branch:
main(or your default branch)
Build & Deploy Settings:
- Build Command:
pip install -r requirements.txt - Start Command:
gunicorn app:app
Python Version: Set
PYTHON_VERSION=3.10.11in Render environment variables (matches .python-version file) - Name:
-
Environment Variables
Add the following environment variables in Render dashboard:
-
Key:
DATABASE_URL -
Value: Use the Internal URL from your PostgreSQL service
-
Key:
PYTHON_VERSION -
Value:
3.13.5
Important - URL Types:
- Internal URL: Use this for
DATABASE_URL(faster, more secure) - External URL: Only use if you need external access to the database
-
-
Deploy
- Click "Create Web Service"
- Render will automatically build and deploy your application
- Your app will be available at the provided URL
| Setting | Value |
|---|---|
| Build Command | pip install -r requirements.txt |
| Start Command | gunicorn app:app |
| Environment | Python 3 |
| Environment Variables | DATABASE_URL, PYTHON_VERSION=3.13.5 |
- Database First: Always create the PostgreSQL service before the web service
- Internal URL: Use the Internal URL from PostgreSQL service for
DATABASE_URL(faster, more secure) - Auto-Deploy: Render automatically deploys when you push to your main branch
- Health Checks: The
/healthendpoint can be used for Render's health check configuration - Environment Variables: Set
DATABASE_URLin Render's dashboard, not in your code - Logs: Check Render's logs if deployment fails
- Description: Homepage with "Hello, World!" message
- Response: HTML page with health status display
- Description: Health check endpoint
- Response: JSON with application and database status
Success Response:
{
"status": "ok",
"db": "ok"
}Database Error Response:
{
"status": "ok",
"db": "error"
}.
├── app.py # Main Flask application
├── templates/
│ └── index.html # Homepage template with JavaScript
├── requirements.txt # Python dependencies
├── .python-version # Python version specification (3.10.11)
└── README.md # This file
-
Database Connection Error
- Verify your
DATABASE_URLis correct - Ensure PostgreSQL server is running
- Check firewall settings
- For Render: Ensure database is accessible from Render's servers
- Verify your
-
Port Already in Use
- Change the port in
app.pyor setPORTenvironment variable - Kill existing processes using the port
- Change the port in
-
Dependencies Installation Issues
- Ensure you're using Python 3.8+
- Try upgrading pip:
pip install --upgrade pip - Use virtual environment:
python -m venv venv && source venv/bin/activate
-
Render Deployment Issues
- Check build logs in Render dashboard
- Ensure
requirements.txtis in the root directory - Verify
gunicornis in requirements.txt - Check that
DATABASE_URLandPYTHON_VERSIONare set in environment variables - Python Version: Ensure
PYTHON_VERSION=3.13.5matches your.python-versionfile
| Variable | Description | Example | Required |
|---|---|---|---|
DATABASE_URL |
PostgreSQL connection string | postgresql://user:pass@host:5432/db |
Yes |
PYTHON_VERSION |
Python version for Render | 3.13.5 |
Yes |
PORT |
Application port (Render sets this) | 5000 |
No |
FLASK_ENV |
Flask environment | development |
No |
PORT: Automatically set by Render (don't override)DATABASE_URL: Must be set in Render dashboardFLASK_ENV: Set toproductionon Render (optional)
When you create a PostgreSQL service on Render, you'll see two connection URLs:
Internal URL (Recommended for DATABASE_URL):
- Format:
postgresql://user:pass@internal-host:5432/database - Use this for your web service - faster and more secure
- Only accessible from other Render services in the same region
External URL:
- Format:
postgresql://user:pass@external-host:5432/database - Only use if you need external access (e.g., from your local machine)
- Slower and less secure than internal URL
- Fork the repository
- Create a feature branch
- Make your changes
- Test locally
- Submit a pull request
This project is open source and available under the MIT License.