Connecting Laravel to Databases (MySQL, SQLite, PostgreSQL)
Almost every Laravel application needs to store and retrieve data, and that starts with a working database connection. Laravel ships with support for several database systems out of the box, and switching between them is largely a matter of configuration rather than code changes.
How Laravel Manages Connections
Database connections are configured in config/database.php, but the actual values are pulled from the .env file so each environment can point to a different database without touching code. A single application can even define multiple named connections if it needs to talk to more than one database.
Supported Drivers
Laravel supports several database drivers natively, each suited to different project needs. The choice mostly comes down to hosting environment, expected scale, and team familiarity.
| Driver | Best For |
|---|---|
| MySQL | Most shared hosting and traditional production setups |
| PostgreSQL | Applications needing advanced data types and strict data integrity |
| SQLite | Local development, testing, and small, single-file applications |
| SQL Server | Enterprise environments already standardized on Microsoft infrastructure |
Key .env Variables for a Connection
DB_CONNECTION— selects the driver, such asmysqlorpgsqlDB_HOSTandDB_PORT— where the database server can be reachedDB_DATABASE— the specific database name to useDB_USERNAMEandDB_PASSWORD— credentials for authenticating the connection
With a connection in place, writing raw SQL for every query would quickly get repetitive — which is exactly the problem Laravel's Eloquent ORM was built to solve.
Example .env Blocks for Each Database
Only the database section of .env changes between drivers. Here are typical values for local development:
# MySQL or MariaDB
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=shop
DB_USERNAME=shop_user
DB_PASSWORD=change-me
# PostgreSQL
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=shop
DB_USERNAME=shop_user
DB_PASSWORD=change-me
# SQLite
DB_CONNECTION=sqlite
# DB_DATABASE omitted: Laravel uses database/database.sqlite
Recent Laravel versions use SQLite by default for new projects, so a fresh install may already point there. For SQLite, the file must exist; create it with touch database/database.sqlite on macOS or Linux, or let the installer create it. If you set DB_DATABASE for SQLite, give an absolute path.
Testing the Connection Step by Step
- Create the empty database and a user with rights on it in MySQL or PostgreSQL.
- Update
.envwith the matching values. - Clear any cached configuration so Laravel reads the new values.
- Run the migrations; if tables are created, the connection works.
php artisan config:clear
php artisan migrate
# Quick check from Tinker
php artisan tinker
>>> DB::connection()->getPdo();
>>> DB::select('select 1 as ok');
Recent releases also provide php artisan db:show, which prints the connection details, database version and table count in one go.
Errors You Are Likely to See and Their Causes
| Message | Usual cause and fix |
|---|---|
could not find driver | The PHP extension is missing. Enable pdo_mysql, pdo_pgsql or pdo_sqlite in php.ini and restart PHP. |
SQLSTATE[HY000] [2002] Connection refused | The database server is not running, or the host or port is wrong. Inside Docker, use the service name, such as mysql, not 127.0.0.1. |
Access denied for user | Wrong username or password, or the user lacks rights on that database. |
Unknown database | The database named in DB_DATABASE has not been created yet. |
| New values ignored | Configuration is cached; run php artisan config:clear. |
When a connection fails, read the full exception rather than just the first line. Laravel includes the driver, host and database name in the message, which usually points straight at the setting that is wrong. Comparing those values with what you see in your database client is often the fastest way to spot a typo.
Using two connections in one app
Every entry in the connections array of config/database.php has a name. You can query a non-default one directly, or bind a model to it:
$rows = DB::connection('pgsql')->table('reports')->get();
class Report extends Model
{
protected $connection = 'pgsql';
}
Keeping Credentials and Data Safe
- Never commit
.envto version control; commit.env.examplewith placeholder values instead. - Give the application user only the privileges it needs, not a database superuser.
- Use a separate database for automated tests, often SQLite in memory, so tests cannot wipe development data.
- In production, run
php artisan config:cacheafter deploying so settings load quickly, and remember to rebuild the cache whenever environment values change.
Ready to master Laravel Training Course?
Join Uncodemy's hands-on Laravel program and build real, production-ready applications.