Adding Customers to Magento in raw SQL

Adding Customers to Magento in raw SQL

October 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Adding Customers to Magento in raw SQL

Magento: Adding Customers via SQL - A Comprehensive Guide

Hello Developers! Today we’re diving into Magento’s customer management system and exploring how to add new customers directly via raw SQL queries. This approach can be particularly useful when you need to create a standalone script or when working with large datasets independent of the Magento codebase.

Understanding Magento’s Customer Tables

Before we start inserting data, let’s first understand the key tables involved in Magento’s customer management:

  • customer_entity: The primary table holding basic customer information like entity_id (the unique identifier), created_at, updated_at, and other general details.
  • customer_group: Represents different groups customers can belong to, like ‘General’, ‘Wholesale’, etc. For new customers, you’ll want to use the default group ID.

The tables we listed earlier store customer attributes based on their data types (datetime, decimal, int, text, varchar):

  • customer_entity_datetime
  • customer_entity_decimal
  • customer_entity_int
  • customer_entity_text
  • customer_entity_varchar

Creating a New Customer via Raw SQL

Let’s assume you have the following customer details to insert:

To create this customer, we’ll need to manually handle the primary key (entity_id), which is auto-incrementing in MySQL.

First, let’s ensure we know what attribute_set_id to use. This value determines which attribute set a customer belongs to. You can fetch it using:

SELECT * FROM eav_attribute_set WHERE attribute_set_name = 'Customer Attribute Set';

Assume the returned ID is 0 for the default customer attribute set.

Now, let’s insert into customer_entity:

INSERT INTO `customer_entity` (
    `entity_type_id`,
    `attribute_set_id`,
    `created_at`,
    `updated_at`,
    `created_in`,
    `email`,
    `firstname`,
    `lastname`
)
VALUES (
    0, -- Customer entity type ID
    0, -- Attribute set ID from previous query
    NOW(),
    NULL,
    0,
    'john.doe@example.com',
    'John',
    'Doe'
);

This will create a new customer record in the customer_entity table.

Populating Other Customer Tables

After inserting into customer_entity, you’ll need to populate the other tables with relevant attribute values:

  • customer_group: If not already created, insert or fetch the ID for the group (usually ‘General’).
  • customer_entity_varchar and other type-specific tables: Insert the corresponding attribute IDs and their respective values for each customer.

The query will look something like this:

INSERT INTO `customer_entity_varchar` (
    `entity_id`,
    `attribute_id`,
    `value`
)
VALUES (
    '123', -- Customer entity ID from previous insert
    '456', -- Attribute ID for the first name field
    'John'
);

Remember to replace '123' and '456' with your actual IDs.

Conclusion

While Magento encourages using its API or data import/export functionalities, there are scenarios where raw SQL queries provide a more direct approach, especially when dealing with large datasets or external systems. The key is understanding the database structure and following proper normalization practices to maintain data consistency.

If you’re looking for more advanced techniques like data migration from an existing system into Magento, I recommend checking out our comprehensive guide on Data Migration in Magento for a step-by-step walkthrough.

Happy coding, and feel free to share your experiences with raw SQL operations in the comments below!

Database Email Customers

Our network