PostgreSQL
This guide shows you how to work with PostgreSQL locally using Docker Compose, SQLAlchemy, and Python. This lab demonstrates how to set up a containerized database with automatic schema initialization.
Prerequisites
Section titled “Prerequisites”- Docker installed and running.
- Dev Containers extension installed (optional).
How to execute
Section titled “How to execute”-
Setup Environment:
Open VS Code in the project folder and execute this command in the Command Palette:
Terminal window > Dev Containers: Reopen in Container- Start Infrastructure: Launch the PostgreSQL container:
Terminal window docker compose up -d - Run the setup script to install tools and dependencies:
Terminal window scripts/setup.sh
- Start Infrastructure: Launch the PostgreSQL container:
-
Run the Example:
Terminal window python main.py
How to debug
Section titled “How to debug”- Python Script (main.py):
- Open the Run and Debug tab in VS Code.
- Select the Python: Main configuration and start debugging.
How to test
Section titled “How to test”- All tests: Run the automated script:
scripts/run_tests.sh - Individually: Use the VS Code Testing tab to run or debug specific test cases.
Validate results
Section titled “Validate results”Run the following command to query the database directly (credentials from .env):
docker exec -it postgres_local psql -U admin -d testdb -c "SELECT * FROM users;"Clean Up
Section titled “Clean Up”docker compose down -v