Interact with the TMI PostgreSQL database for queries and administration. Use when asked to show data, check tables, or run SQL queries.
You are helping the user interact with the TMI PostgreSQL database.
IMPORTANT: All database connection information and credentials are stored in config-development.yml:
PostgreSQL command-line tools (psql) are NOT installed on the host machine.
You MUST use docker exec to run psql commands inside the tmi-postgresql container.
To start an interactive psql session:
docker exec -it tmi-postgresql psql -U tmi_dev -d tmi_dev
To run a single SQL query:
docker exec tmi-postgresql psql -U tmi_dev -d tmi_dev -c "YOUR SQL QUERY HERE"
To execute SQL from a file:
docker exec -i tmi-postgresql psql -U tmi_dev -d tmi_dev < /path/to/file.sql
Or using heredoc:
docker exec -i tmi-postgresql psql -U tmi_dev -d tmi_dev <<'EOF'
YOUR SQL QUERY HERE
EOF
docker exec tmi-postgresql psql -U tmi_dev -d tmi_dev -c "\dt"
docker exec tmi-postgresql psql -U tmi_dev -d tmi_dev -c "\d table_name"
docker exec tmi-postgresql psql -U tmi_dev -d tmi_dev -c "SELECT COUNT(*) FROM table_name;"
docker exec tmi-postgresql psql -U tmi_dev -d tmi_dev -c "SELECT * FROM table_name LIMIT 10;"
Migrations are located in auth/migrations/ directory. See the migration commands in the Makefile or use:
make migrate
To reset the database (drop and recreate schema):
make heroku-reset-db # Works for local database too
To clear automatically generated test data out of the development postgresql database without dropping it and recreating it:
make test-db-clean
Key tables in the TMI database:
users - User accounts and authenticationthreat_models - Top-level threat model entitiesdiagrams - Threat model diagrams (DFD, etc.)cells - Diagram cells (nodes and edges)threats - Identified threatsdocuments - Document attachmentsrepositories - Code repository linksnotes - Text notesassets - Asset inventory itemsmetadata - Flexible key-value metadata for entitiesdocker ps --filter "name=tmi-postgresql"
docker exec -i tmi-postgresql psql -U tmi_dev -d tmi_dev <<'EOF'
SELECT * FROM table_name WHERE condition = 'value';
EOF
If you encounter errors:
make start-database or make start-devdocker psconfig-development.ymlmake migrateconfig-development.yml are for local development onlydev123) is intentionally simple for local developmentdocker exec tmi-postgresql psql -U tmi_dev -d tmi_dev -c "
SELECT id, name, owner, created_at
FROM threat_models
ORDER BY created_at DESC
LIMIT 5;
"
docker exec tmi-postgresql psql -U tmi_dev -d tmi_dev -c "
SELECT a.id, a.name, a.type, tm.name as threat_model
FROM assets a
JOIN threat_models tm ON a.threat_model_id = tm.id
WHERE a.type = 'software';
"
docker exec tmi-postgresql psql -U tmi_dev -d tmi_dev -c "
SELECT version, applied_at
FROM schema_migrations
ORDER BY version;
"
When the user asks to:
make heroku-reset-db or specific DELETE queriesAlways show the user the SQL query you're about to execute before running it, especially for INSERT, UPDATE, or DELETE operations.