Database operations with docker-local - create, connect, backup, restore for MySQL and PostgreSQL
This skill manages database operations in docker-local:
| Service | Version | Port | Use Case |
|---|---|---|---|
| MySQL | 9.1 | 3306 | Traditional relational database |
| PostgreSQL | 17 | 5432 | Advanced features, pgvector for AI |
| Redis | 8 | 6379 | Cache, sessions, queues |
Before ANY docker-local command, verify installation:
which docker-local > /dev/null 2>&1
If docker-local is NOT found:
composer global require mwguerra/docker-localexport PATH="$HOME/.composer/vendor/bin:$PATH"docker-local initwhich docker-local && docker-local --version# MySQL CLI
docker-local db:mysql
# PostgreSQL CLI
docker-local db:postgres
# Redis CLI
docker-local db:redis
# Create MySQL database
docker-local db:create myapp
# Creates:
# - myapp (main database)
# - myapp_testing (for tests)
# Dump current project database
docker-local db:dump
# Dump specific database
docker-local db:dump myapp
# Output goes to: myapp_YYYY-MM-DD_HH-MM-SS.sql
Manual backup commands:
# MySQL
docker exec mysql mysqldump -u laravel -psecret myapp > backup.sql
# PostgreSQL
docker exec postgres pg_dump -U laravel myapp > backup.sql
# Restore from SQL file
docker-local db:restore backup.sql
# Restore to specific database
docker-local db:restore backup.sql myapp
Manual restore commands:
# MySQL
docker exec -i mysql mysql -u laravel -psecret myapp < backup.sql
# PostgreSQL
docker exec -i postgres psql -U laravel myapp < backup.sql
# Fresh migration with seeds
docker-local db:fresh
# Regular migration
docker-local tinker <<< "Artisan::call('migrate')"
# Or via artisan
docker exec -w /var/www/myapp php php artisan migrate
Host: mysql (inside Docker) / localhost (from host)
Port: 3306
User: laravel (or root)
Password: secret
Host: postgres (inside Docker) / localhost (from host)
Port: 5432
User: laravel
Password: secret
Host: redis (inside Docker) / localhost (from host)
Port: 6379
Password: (none)
For GUI tools like TablePlus, DBeaver, or DataGrip:
Host: localhost (or 127.0.0.1)
Port: 3306
User: laravel (or root)
Password: secret
Database: myapp
Host: localhost (or 127.0.0.1)
Port: 5432
User: laravel
Password: secret
Database: myapp
Laravel .env should use Docker service names:
# MySQL
DB_HOST=mysql
DB_PORT=3306
# PostgreSQL
DB_HOST=postgres
DB_PORT=5432
# Redis
REDIS_HOST=redis
REDIS_PORT=6379
Important: Use service names (mysql, postgres, redis), NOT localhost!
PostgreSQL 17 includes pgvector for AI embeddings:
-- Extension is pre-installed
-- Just use it in your tables:
CREATE TABLE items (
id SERIAL PRIMARY KEY,
embedding vector(1536)
);
-- Similarity search
SELECT * FROM items ORDER BY embedding <-> '[...]' LIMIT 10;
Redis has 16 databases (0-15). Docker-local allocates 3 per project:
| Project | Cache DB | Session DB | Queue DB |
|---|---|---|---|
| 1st project | 0 | 1 | 2 |
| 2nd project | 3 | 4 | 5 |
| 3rd project | 6 | 7 | 8 |
| 4th project | 9 | 10 | 11 |
| 5th project | 12 | 13 | 14 |
Check Redis usage:
# Connect to Redis
docker-local db:redis
# List all keys in current DB
KEYS *
# Switch databases
SELECT 0
SELECT 3
# Count keys per database
INFO keyspace
# Check if container is running
docker-local status
# Check database logs
docker-local logs mysql
docker-local logs postgres
# Test connection
docker exec mysql mysqladmin ping -h localhost -u root -psecret
docker exec postgres pg_isready -U laravel
# MySQL - grant privileges
docker exec mysql mysql -u root -psecret -e "GRANT ALL ON myapp.* TO 'laravel'@'%';"
# PostgreSQL - check ownership
docker exec postgres psql -U laravel -c "\l"
# List existing databases
# MySQL
docker exec mysql mysql -u root -psecret -e "SHOW DATABASES;"
# PostgreSQL
docker exec postgres psql -U laravel -c "\l"
# Create missing database
docker-local db:create myapp
# Drop and recreate
# MySQL
docker exec mysql mysql -u root -psecret -e "DROP DATABASE myapp; CREATE DATABASE myapp;"
# PostgreSQL
docker exec postgres psql -U laravel -c "DROP DATABASE myapp; CREATE DATABASE myapp;"
# Then migrate
docker-local db:fresh
-- Show databases
SHOW DATABASES;
-- Show tables
USE myapp;
SHOW TABLES;
-- Show table structure
DESCRIBE users;
-- Export specific table
docker exec mysql mysqldump -u laravel -psecret myapp users > users.sql
-- List databases
\l
-- Connect to database
\c myapp
-- List tables
\dt
-- Describe table
\d users
-- Show running queries
SELECT * FROM pg_stat_activity;
# List all keys
KEYS *
# Get key type
TYPE mykey
# Get string value
GET mykey
# Get hash
HGETALL myhash
# Clear current database
FLUSHDB
# Clear all databases (careful!)
FLUSHALL
# Monitor commands in real-time
MONITOR
$ARGUMENTS