postgres-service/runner/readme.md

2.8 KiB

PostgreSQL Database Tester

This is a Python-based testing tool for PostgreSQL databases. It uses SQLAlchemy for ORM and Alembic for database migrations.

Features

  • Test PostgreSQL database connection
  • Create database schema using SQLAlchemy models
  • Run Alembic migrations
  • Insert test data
  • Query and display database content

Setup

  1. Make sure you have a PostgreSQL server running (locally or via Docker)

  2. Install dependencies:

    # Using uv (already done)
    uv add alembic sqlalchemy-mixins psycopg2-binary requests
    
  3. Set up environment variables (optional):

    export DATABASE_URL="postgresql://postgres:password@localhost:5432/postgres"
    

Project Structure

./
├── alembic/              # Alembic migration files
│   ├── versions/         # Migration version files
│   ├── env.py            # Alembic environment configuration
│   └── script.py.mako    # Migration script template
├── app/                  # Application package
│   ├── models/           # SQLAlchemy models
│   │   ├── __init__.py   # Models package initialization
│   │   ├── user.py       # User model
│   │   └── post.py       # Post model
│   └── __init__.py       # Application initialization
├── alembic.ini           # Alembic configuration
├── db_tester.py          # Database testing script
└── README.md             # This file

Usage

Test Database Connection

python db_tester.py --connection

Run Alembic Migration

python db_tester.py --migration

Create Database Tables

python db_tester.py --create-tables

Insert Test Data

python db_tester.py --insert-data

Query Test Data

python db_tester.py --query-data

Run Stress Test with 100 Concurrent Connections

python db_tester.py --stress-test

You can also specify a custom number of threads:

python db_tester.py --stress-test --threads 50

Run All Tests

python db_tester.py --all

Connecting to PostgreSQL Container

If you're using the PostgreSQL Docker container from the parent project, you can connect to it by setting the appropriate DATABASE_URL:

export DATABASE_URL="postgresql://postgres:password@localhost:5432/postgres"

Replace password with the actual password from your .env file.

Customizing Models

You can modify the SQLAlchemy models in the app/models/ directory to match your specific database schema requirements.

Troubleshooting

  • Connection Issues: Make sure your PostgreSQL server is running and accessible
  • Migration Errors: Check the Alembic version files for any issues
  • Model Errors: Ensure your SQLAlchemy models are correctly defined