Skip to main content

Introduction

Laravel officially supports the following databases:
  • MySQL / MariaDB
  • PostgreSQL
  • SQLite
  • SQL Server
You can use the same connection configuration whether you’re using raw SQL, the query builder, or the Eloquent ORM.
This page covers the prerequisites of database connections. For practical queries, see Query Builder. For schema management, see Migrations. For initial data population, see Seeding.

Configuration

Database configuration is centralized in config/database.php. You choose the default connection name with default, and define the details of each connection under connections.
In .env, at minimum set the following:
When using SQLite, set DB_CONNECTION=sqlite and the path in DB_DATABASE.

Separating read and write connections

If you want to separate reads (SELECT) and writes (INSERT / UPDATE / DELETE) onto different hosts, configure read and write within the same connection.
Setting sticky to true pins reads after a write to the write connection within the same request.
In setups where replicas lag behind, enabling sticky reduces the risk of reading stale data immediately after a write.

Preventing stale reads on subsequent requests

sticky only applies within the same request. To read freshly written data on subsequent requests in the same session as well, add the PreventStaleDatabaseReads middleware to the web middleware group.
The middleware records writes to the default database connection in the session. By default, requests made within 2 seconds of a write use the write connection for reads on that connection. To change the duration, use for.
This middleware depends on the session, so it must run after the StartSession middleware. It does not affect reads from other sessions or other database connections.

Reading from the write connection temporarily

To pin reads inside a callback to the write connection, use the connection’s usingWritePdo method.
This also applies to Eloquent and relationship queries that use the same connection inside the callback. When the callback finishes, the connection’s read setting is restored whether or not an exception was thrown.

Pooled PostgreSQL connections

When using a managed PostgreSQL service that provides transaction-mode connection pooling (such as PgBouncer), configure the pooled and direct options.
When pooled is enabled, Laravel automatically routes traffic. If you want to explicitly use the direct connection in application code, add the ::direct suffix to the connection name.

Multiple database connections

You can define multiple connections in config/database.php and switch between them with DB::connection().

Executing SQL queries

The DB facade has dedicated methods for each query type.
Never concatenate user input directly into SQL strings. Always use binding parameters to prevent SQL injection.

Query listening

When you want to track the SQL that is executed, register DB::listen() in a service provider’s boot() method.

Transactions

To treat multiple updates as a single unit, use DB::transaction(). Laravel automatically rolls back if an exception is thrown.
If you need deadlock retries, you can specify attempts.
If you want manual control, use beginTransaction / rollBack / commit.

Next steps

Query Builder

Learn how to safely build queries using your connection configuration.

Migrations

Learn how to version-control the schema of your connected database.

Seeding

Learn how to populate development and test data in a reproducible way.
Last modified on October 20, 2026