subhanraj / laravel-db-provisioner
Automate local database and user creation with a single Artisan command for Laravel
Package info
github.com/SubhanRaj/laravel-db-provisioner
pkg:composer/subhanraj/laravel-db-provisioner
Requires
- php: ^8.1
- illuminate/console: >=8.0
- illuminate/support: >=8.0
README
A Laravel package that automates local database and user creation for new project clones. Perfect for any local development environment (Valet, XAMPP, Laragon, Homebrew, Docker Compose, etc.) using MySQL or MariaDB.
Features
- 🔧 Automatic
.envfile creation from.env.example - 🔐 Generates secure 16-character database passwords
- 📝 Auto-generates database names from your APP_NAME or directory name
- 🗄️ Creates database, users, and grants privileges via raw PDO
- 💻 Works with any MySQL/MariaDB setup (XAMPP, Laragon, Valet, Homebrew, etc.)
- 🪟 Cross-platform: Windows, macOS, Linux
- ⚡ Single command setup:
php artisan db:provision
Installation
Via Composer
composer require subhanraj/laravel-db-provisioner
The package will auto-register via Laravel's package discovery.
Usage
After installation, run:
php artisan db:provision
What the command does:
-
✓ Checks if
.envexists; copies from.env.exampleif needed -
✓ Generates a clean database name (e.g.,
myapp_local) -
✓ Generates a secure 16-character password
-
✓ Updates
.envwithDB_DATABASE,DB_USERNAME, andDB_PASSWORD -
✓ Prompts for database admin username (defaults to
root) -
✓ Prompts for database admin password (allows blank for no-password setups)
-
✓ Connects to your database using the provided credentials and executes:
CREATE DATABASE IF NOT EXISTS {DB_DATABASE}- Creates database users for both
127.0.0.1andlocalhost - Grants all privileges on the database to those users
- Flushes privileges
-
✓ Outputs a success message with the new credentials
Next Steps
After running the command, execute:
php artisan key:generate php artisan migrate
Configuration Requirements
MySQL/MariaDB Server
This package requires a running MySQL or MariaDB server. The package will:
- Use the database host and port from your
.envfile (defaults to127.0.0.1:3306) - Prompt for the admin username (defaults to
root) - Accept blank passwords for no-password setups (common in local development)
Supported Environments
This package works with any local MySQL/MariaDB setup:
- XAMPP - Uses
rootuser with no password by default - Laragon - Uses
rootuser with no password by default - Laravel Valet - Works with MySQL/MariaDB started via Valet
- Homebrew - Works with Homebrew-installed MySQL/MariaDB
- Docker Compose - Works with containers if the host is accessible
- Windows Subsystem for Linux (WSL) - Works with any WSL database setup
Environment Variables
The package reads and modifies the following .env variables:
DB_CONNECTION=mysql # Connection type (mysql or mariadb) DB_HOST=127.0.0.1 # Database host (customizable, defaults to 127.0.0.1) DB_PORT=3306 # Database port (customizable, defaults to 3306) DB_DATABASE= # Generated by command DB_USERNAME= # Generated by command (same as DB_DATABASE) DB_PASSWORD= # Generated securely (16 chars)
Security Notes
- Passwords are generated with a mix of uppercase, lowercase, numbers, and special characters
- Uses
file_get_contents()andpreg_replace()for safe.envfile updates - Raw PDO connection ensures no Laravel query builder interference
- Admin credentials are taken as input (not stored) and used only for database setup
- After provisioning, the command disconnects from the admin account
Troubleshooting
"Failed to connect to [host]:[port]"
- Ensure your database server is running
- XAMPP: Check Apache/MySQL in Control Panel
- Laragon: Check Laragon is running
- Valet: Ensure MySQL is started (
valet use mysqlor equivalent) - Homebrew:
brew services list | grep mysqlorbrew services list | grep mariadb
- Verify the DB_HOST and DB_PORT in your
.envfile match your setup - Check that your firewall isn't blocking the connection
"Access denied for user 'root'@'127.0.0.1'"
- Verify you entered the correct admin username and password
- For XAMPP/Laragon: Default is usually
rootwith no password (leave blank when prompted) - For Homebrew: If you ran
mysql_secure_installation, use the password you set - Try connecting manually:
mysql -h 127.0.0.1 -u root -p
".env.example file not found"
- Ensure your Laravel project has a
.env.examplefile in the root - Create one from your
.envif needed:cp .env .env.example
"Permission denied" or "GRANT failed"
- Ensure the admin user has privileges to create users and databases
- Try connecting manually to verify admin access works
- Check your database server logs for detailed error messages
Command hangs or times out
- Your database server may not be responding
- Check your firewall/antivirus isn't blocking connections
- Try connecting manually first to isolate the issue
License
MIT
Support
For issues, questions, or contributions, please open an issue on GitHub or see CONTRIBUTING.md.