Execute SQL queries and data modifications on PostgreSQL databases running in Docker containers. Auto-detects running PostgreSQL containers and executes SELECT, INSERT, UPDATE, and DELETE operations. Use when working with PostgreSQL databases in containers for querying data, modifying records, analyzing database contents, or performing ad-hoc database operations. Triggers on database queries, SQL operations, data inspection, or PostgreSQL interactions.
Execute SQL queries and data modifications on PostgreSQL databases running in Docker containers with automatic container detection.
Execute a query using the automated script:
scripts/query_db.sh <database_name> "<sql_query>" [container_id]
Examples:
# Auto-detect container and query users table
scripts/query_db.sh order_development "SELECT * FROM users LIMIT 10"
# Insert data
scripts/query_db.sh order_development "INSERT INTO logs (level, message) VALUES ('INFO', 'Test log')"
# Update records
scripts/query_db.sh order_development "UPDATE users SET active = true WHERE id = 123"
# Specify container explicitly
scripts/query_db.sh order_development "SELECT * FROM orders WHERE status = 'pending'" c091f5a68780
Main query execution script with auto-detection of PostgreSQL containers.
Features:
Arguments:
database_name - Target database (e.g., order_development, postgres)sql_query - SQL query to execute (must be quoted)container_id - Optional container ID (auto-detected if omitted)Connection Details:
postgres (default PostgreSQL superuser)docker exec to running containersList all running PostgreSQL containers with database information.
scripts/list_containers.sh
Shows:
Use this when:
# View table structure
scripts/query_db.sh mydb "SELECT column_name, data_type FROM information_schema.columns WHERE table_name = 'users'"
# View recent records
scripts/query_db.sh mydb "SELECT * FROM orders ORDER BY created_at DESC LIMIT 20"
# Count records
scripts/query_db.sh mydb "SELECT COUNT(*) FROM users WHERE active = true"
# Insert test data
scripts/query_db.sh mydb "INSERT INTO users (email, name) VALUES ('[email protected]', 'Test User') RETURNING id"
# Update records
scripts/query_db.sh mydb "UPDATE products SET price = 29.99 WHERE id = 456"
# Delete old records
scripts/query_db.sh mydb "DELETE FROM logs WHERE created_at < NOW() - INTERVAL '30 days'"
# List all tables
scripts/query_db.sh mydb "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'"
# Check table sizes
scripts/query_db.sh mydb "SELECT pg_size_pretty(pg_total_relation_size('users')) as size"
# Active connections
scripts/query_db.sh mydb "SELECT COUNT(*) FROM pg_stat_activity WHERE state != 'idle'"
For multi-line queries or queries with special characters, use proper quoting:
scripts/query_db.sh mydb "
SELECT
users.email,
COUNT(orders.id) as order_count,
SUM(orders.total) as revenue
FROM users
LEFT JOIN orders ON users.id = orders.user_id
GROUP BY users.id, users.email
ORDER BY revenue DESC
LIMIT 10
"
When multiple containers are running, specify the container:
# List containers first
scripts/list_containers.sh
# Query specific container
scripts/query_db.sh order_development "SELECT * FROM users" c091f5a68780
For related operations, chain multiple queries:
# Create and populate table
scripts/query_db.sh mydb "CREATE TABLE temp_data (id SERIAL, value TEXT)"
scripts/query_db.sh mydb "INSERT INTO temp_data (value) VALUES ('test1'), ('test2')"
scripts/query_db.sh mydb "SELECT * FROM temp_data"
See references/common_queries.md for comprehensive examples of:
Load this reference when working on complex queries or need query pattern examples.
# Verify PostgreSQL containers are running
docker ps | grep postgres
# Start a container if needed
docker start <container_name>
The script will list all found containers. Either:
docker stop <container_id># List available databases
scripts/list_containers.sh
# Create database if needed
scripts/query_db.sh postgres "CREATE DATABASE mydb"
Ensure Docker is running and accessible:
docker ps # Should list containers without sudo
This skill uses the same connection pattern as the postgres-backup-restore skill:
postgres (default superuser)docker exec -it <container_id> psqldocker ps filtering PostgreSQL imagesLIMIT for large tables to avoid overwhelming outputRETURNING * to see affected rowsWorks well with:
postgres-backup-restore - Use this skill to query restored backup data