Beyond the Schema: A Practical Guide to Querying and Interacting with SQLite, MySQL, & PostgreSQL¶
Building on our analysis of cross-engine schema definitions, this guide focuses on daily database operation: query execution mechanics, CLI diagnostic commands, script piping, and Dockerized networking nuances across SQLite, MySQL, and PostgreSQL.
This reference is grounded in practical scripts from the Examination Management System (EMS DB) project repository.
1. CLI Shell Access & Connection Flags¶
Each RDBMS provides a dedicated terminal client with specific formatting and debugging flags:
# Direct local connection with input echo (-a) and error display (-b)
psql -a -b -d ems -U postgres
# Connect to a containerized instance from an application service
psql -h db -U postgres -d ems
Tip: Use
~/.pgpass(hostname:port:database:username:password) withchmod 600for secure, passwordless authentication in local development.
2. Executing SQL Scripts from Files¶
Running batch DDL migrations or query test benches from external .sql files:
# From inside the mysql prompt:
source ./queries.sql
# Via shell stdin piping:
mysql -tv -u root -psecret ems < ./queries.sql
Note: Because
mysql-connector-pythonlacks native support for theDELIMITERdirective required by complex trigger blocks, executing schema migrations via the CLI client is the recommended production practice.
3. Resetting Auto-Increment Sequences¶
When wiping test tables (DELETE FROM students;), resetting the primary key counter requires engine-specific operations:
4. Shell Diagnostic & Inspection Commands¶
Inspecting catalog objects (tables, indexes, views) from within interactive database shells:
\dt -- List all tables in current schema
\di -- List all indexes
\dv -- List all views
\d+ <table> -- Inspect detailed table definition, triggers, and constraints
Information Schema Alternative:
5. Dockerized Multi-Database Orchestration¶
In reproducible testing environments, database services run inside isolated Docker networks.
# Sample Multi-RDBMS Docker Compose Architecture
services:
app:
image: python:3.12-slim
depends_on:
- postgres-db
- mysql-db
volumes:
- ./:/workspace
postgres-db:
image: postgres:17-alpine
environment:
POSTGRES_DB: ems
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
mysql-db:
image: mysql:8.4
environment:
MYSQL_DATABASE: ems
MYSQL_ROOT_PASSWORD: secret
CLI Container Exec Patterns¶
Quick Reference Summary¶
| Operation | PostgreSQL | MySQL | SQLite |
|---|---|---|---|
| CLI Binary | psql |
mysql / mysqlsh |
sqlite3 |
| Run Script (Prompt) | \i queries.sql |
source queries.sql |
.read queries.sql |
| Reset Sequence | ALTER SEQUENCE ... RESTART WITH 1; |
ALTER TABLE ... AUTO_INCREMENT = 1; |
DELETE FROM sqlite_sequence ... |
| Inspect DDL | \d+ table_name |
SHOW CREATE TABLE table_name; |
.schema table_name |
| Execution Plan | EXPLAIN ANALYZE SELECT ...; |
EXPLAIN SELECT ...; |
EXPLAIN QUERY PLAN SELECT ...; |
| Docker Hostname | DNS service name (postgres-db) |
DNS service name (mysql-db) |
Local file path / mount |
Conclusion & Series Navigation¶
Understanding both the schema syntax (Part 1) and the operational tooling (Part 2) ensures seamless database migrations and resilient CI/CD pipelines across different relational engines.