Skip to main content
Backend App supports multiple database drivers including SQLite, MySQL, MariaDB, PostgreSQL, and SQL Server. The default configuration uses SQLite for quick setup and development.

Default Configuration

By default, the application uses SQLite with a database file stored at database/database.sqlite.
.env
DB_CONNECTION=sqlite
SQLite is perfect for development and small applications. No additional setup or server configuration required.

Database Setup

Initial Setup

When setting up the application for the first time, the database file is automatically created:
composer setup
This runs the complete setup including migrations:
  1. Installs dependencies
  2. Copies .env.example to .env
  3. Generates application key
  4. Runs database migrations
  5. Installs and builds frontend assets

Manual Migration

To run migrations manually:
php artisan migrate
To run migrations with force (useful in production):
php artisan migrate --force

Database Drivers

Configuration

SQLite requires minimal configuration. The database is stored as a single file.
.env
DB_CONNECTION=sqlite
The database file location is configured in config/database.php:
'sqlite' => [
    'driver' => 'sqlite',
    'url' => env('DB_URL'),
    'database' => env('DB_DATABASE', database_path('database.sqlite')),
    'prefix' => '',
    'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],

Creating the Database File

If the database file doesn’t exist, create it:
touch database/database.sqlite
SQLite is great for development, testing, and small applications. For production applications with high concurrency, consider MySQL or PostgreSQL.

Configuration

Update your .env file with MySQL connection details:
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=your_password

Connection Options

The MySQL configuration in config/database.php includes:
'mysql' => [
    'driver' => 'mysql',
    'url' => env('DB_URL'),
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'laravel'),
    'username' => env('DB_USERNAME', 'root'),
    'password' => env('DB_PASSWORD', ''),
    'unix_socket' => env('DB_SOCKET', ''),
    'charset' => env('DB_CHARSET', 'utf8mb4'),
    'collation' => env('DB_COLLATION', 'utf8mb4_unicode_ci'),
    'prefix' => '',
    'prefix_indexes' => true,
    'strict' => true,
    'engine' => null,
],

Creating the Database

Create the database using MySQL CLI:
mysql -u root -p
CREATE DATABASE laravel CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Additional MySQL Variables

.env
DB_SOCKET=/path/to/mysql.sock  # For Unix socket connections
DB_CHARSET=utf8mb4
DB_COLLATION=utf8mb4_unicode_ci
MYSQL_ATTR_SSL_CA=/path/to/ca-cert.pem  # For SSL connections

Configuration

MariaDB uses the same connection settings as MySQL with a different driver:
.env
DB_CONNECTION=mariadb
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=your_password
MariaDB is a drop-in replacement for MySQL with enhanced performance and features.

Configuration

Configure PostgreSQL in your .env file:
.env
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=laravel
DB_USERNAME=postgres
DB_PASSWORD=your_password

Connection Options

'pgsql' => [
    'driver' => 'pgsql',
    'url' => env('DB_URL'),
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '5432'),
    'database' => env('DB_DATABASE', 'laravel'),
    'username' => env('DB_USERNAME', 'root'),
    'password' => env('DB_PASSWORD', ''),
    'charset' => env('DB_CHARSET', 'utf8'),
    'prefix' => '',
    'prefix_indexes' => true,
    'search_path' => 'public',
    'sslmode' => env('DB_SSLMODE', 'prefer'),
],

Creating the Database

psql -U postgres
CREATE DATABASE laravel WITH ENCODING 'UTF8';

Additional PostgreSQL Variables

.env
DB_CHARSET=utf8
DB_SSLMODE=prefer  # Options: disable, allow, prefer, require, verify-ca, verify-full

Configuration

For Microsoft SQL Server:
.env
DB_CONNECTION=sqlsrv
DB_HOST=localhost
DB_PORT=1433
DB_DATABASE=laravel
DB_USERNAME=sa
DB_PASSWORD=your_password

Additional Options

.env
DB_CHARSET=utf8
# DB_ENCRYPT=yes
# DB_TRUST_SERVER_CERTIFICATE=false

Migration Files

The application includes the following migrations in database/migrations/:

Core Laravel Migrations

  • 0001_01_01_000001_create_cache_table.php - Cache and cache locks tables
  • 0001_01_01_000002_create_jobs_table.php - Queue jobs, job batches, and failed jobs

Application Migrations

  • 2026_02_27_202300_create_institutions_table.php - Institutions
  • 2026_02_27_210506_create_users_table.php - Users
  • 2026_02_27_214046_section.php - Sections
  • 2026_03_01_214910_registration.php - Registrations
  • 2026_03_01_233421_create_units_table.php - Units
  • 2026_03_01_235915_create_topics_table.php - Topics
  • 2026_03_02_002017_create_resources_table.php - Resources
  • 2026_03_02_011220_create_questions_table.php - Questions
  • 2026_03_05_181016_create_response_options_table.php - Response options
  • 2026_03_05_183245_evaluations.php - Evaluations
  • 2026_03_05_191516_eval_preguntas.php - Evaluation questions
Migrations are run in chronological order based on their timestamp prefix.

Migration Commands

Run Migrations

php artisan migrate

Run Migrations (Production)

php artisan migrate --force
The --force flag is required to run migrations in production when APP_ENV=production.

Check Migration Status

php artisan migrate:status

Rollback Last Migration

php artisan migrate:rollback

Rollback All Migrations

php artisan migrate:reset

Rollback and Re-run All Migrations

php artisan migrate:refresh

Drop All Tables and Re-run Migrations

php artisan migrate:fresh
migrate:fresh will drop all tables. Use with caution in production.

Database Connection Testing

Test your database connection:
php artisan db:show
This displays:
  • Database connection name
  • Database driver
  • Database version
  • Number of open connections

Redis Configuration

The application supports Redis for caching, queues, and sessions.
.env
REDIS_CLIENT=phpredis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

Redis Configuration in config/database.php

'redis' => [
    'client' => env('REDIS_CLIENT', 'phpredis'),
    
    'default' => [
        'url' => env('REDIS_URL'),
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'username' => env('REDIS_USERNAME'),
        'password' => env('REDIS_PASSWORD'),
        'port' => env('REDIS_PORT', '6379'),
        'database' => env('REDIS_DB', '0'),
    ],
    
    'cache' => [
        'url' => env('REDIS_URL'),
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'username' => env('REDIS_USERNAME'),
        'password' => env('REDIS_PASSWORD'),
        'port' => env('REDIS_PORT', '6379'),
        'database' => env('REDIS_CACHE_DB', '1'),
    ],
],

Database URL Connection

You can use a database URL instead of individual connection parameters:
.env
DB_URL=mysql://user:password@localhost:3306/database
This is useful for platforms like Heroku that provide database credentials as a single URL.

Migrations Table

Laravel tracks applied migrations in a table (default: migrations):
config/database.php
'migrations' => [
    'table' => 'migrations',
    'update_date_on_publish' => true,
],

Performance Optimization

Connection Pooling

For high-traffic applications, consider using connection pooling:
  • PgBouncer for PostgreSQL
  • ProxySQL for MySQL/MariaDB
  • Redis Sentinel for Redis high availability

Query Optimization

# Enable query logging in local development
php artisan db:monitor
Use database indexes on frequently queried columns to improve performance.

Troubleshooting

Connection Refused

  1. Verify database server is running
  2. Check host and port settings
  3. Verify firewall rules
  4. Check database credentials

Permission Denied

  1. Verify database user has necessary privileges
  2. Grant privileges: GRANT ALL PRIVILEGES ON database.* TO 'user'@'localhost';

SQLite File Not Found

touch database/database.sqlite
chmod 664 database/database.sqlite
Ensure the web server user has write permissions to the database/ directory.

Build docs developers (and LLMs) love