#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →

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.

DriverBest For
MySQLMost shared hosting and traditional production setups
PostgreSQLApplications needing advanced data types and strict data integrity
SQLiteLocal development, testing, and small, single-file applications
SQL ServerEnterprise environments already standardized on Microsoft infrastructure

Key .env Variables for a 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

  1. Create the empty database and a user with rights on it in MySQL or PostgreSQL.
  2. Update .env with the matching values.
  3. Clear any cached configuration so Laravel reads the new values.
  4. 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

MessageUsual cause and fix
could not find driverThe PHP extension is missing. Enable pdo_mysql, pdo_pgsql or pdo_sqlite in php.ini and restart PHP.
SQLSTATE[HY000] [2002] Connection refusedThe 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 userWrong username or password, or the user lacks rights on that database.
Unknown databaseThe database named in DB_DATABASE has not been created yet.
New values ignoredConfiguration 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 .env to version control; commit .env.example with 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:cache after 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.

Explore Course