How to connect to the database in Magento?

How to connect to the database in Magento?

August 21, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to connect to the database in Magento?

Connecting to the Database in Magento: A Comprehensive Guide

Hello, fellow Magento developers! Today, we’re going to explore how to connect your custom modules to the database right from the start of each page request. This is a common requirement when building extensions that need access to all tables.

Understanding the Default Magento Setup

Before we dive into the code, let’s quickly recap how Magento handles the database connection by default. When you install or upgrade Magento via composer using the command composer require magento/product-community-edition --no-update, it automatically connects to your MySQL database and performs all necessary setup.

This process is managed through a set of configuration files located in the root directory, especially the app/etc/config.php file. This file contains an array of databases that Magento will use for its various connections (e.g., db, dbwrite, etc.).

Connecting Your Custom Module to the Database

Now let’s connect your custom module to this existing setup. Here are the steps:

  1. Create a new module: If you haven’t already, create a new module in the app/code/Vendor/Module directory.

  2. Update the config.xml file: In your module’s etc/ folder, create or update the config.xml file to include the necessary database configuration.

Here’s an example of what this might look like:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Module">
        <sequence>
            <module name="Magento_Backend"/>
        </sequence>
    </module>
</config>
  1. Create your database scripts: Create the necessary SQL files (e.g., sql/install-1.0.0.sql, sql/upgrade-1.0.1-1.0.2.sql) in your module’s root directory to set up the required tables and relationships.

  2. Use the Magento Core Database Classes: Instead of creating a custom config.php file, you should use the built-in Magento classes for all database operations. These include Magento\Framework\App\ResourceConnection, which provides access to the database connection.

Here’s an example of how you might use this in your module:

namespace Vendor\Module\Controller\Index;

use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\ResultFactory;

class Index implements \Magento\Framework\App\ActionInterface
{
    protected $_resourceConnection;

    public function __construct(
        RequestInterface $request,
        ResultFactory $resultFactory,
        \Magento\Framework\App\ResourceConnection $resourceConnection
    ) {
        $this->request = $request;
        $this->resultFactory = $resultFactory;
        $this->_resourceConnection = $resourceConnection;
    }

    public function execute()
    {
        // Use $_resourceConnection to perform your database operations here
        $connection = $this->_resourceConnection->getConnection();
        $sql = "SELECT * FROM catalog_product_entity";
        $products = $connection->fetchAll($sql);

        return $this->resultFactory->create(ResultFactory::TYPE_PAGE);
    }
}

Conclusion

And that’s it! By following these steps and using the built-in Magento classes, you can easily connect your custom modules to the database without having to create a custom config.php file.

Remember, the key is to leverage the existing setup managed by the app/etc/config.php file. This ensures your module plays nicely with other extensions and the core Magento functionality.

For more advanced topics in working with databases in Magento, I recommend checking out this detailed guide on working with database connections at Laravel Company. It provides a deeper dive into the available classes and methods for interacting with MySQL from within your custom code.

Happy coding!

Catalog Database Extensions Configuration

Our network