How to Import CSV Files in MariaDB

Galaxy Glossary

How do I import a CSV file into a MariaDB table?

LOAD DATA INFILE quickly imports CSV data directly into a MariaDB table.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Description

What SQL command imports CSV files in MariaDB?

Use LOAD DATA [LOCAL] INFILE. It streams a CSV file straight into a table, handling field separation, quoting, and line termination automatically.

How to prepare the CSV file?

Confirm the CSV columns align with the target table order—Customers.csv should list id,name,email,created_at. Save with UTF-8 encoding and Unix line breaks for predictable parsing.

How to grant file privileges before import?

Run GRANT FILE ON *.* TO 'app_user'@'%' IDENTIFIED BY 'strong_pw'; then FLUSH PRIVILEGES;.Without the FILE privilege, MariaDB blocks access to server-side CSV files.

How to run LOAD DATA INFILE step by step?

Place Customers.csv in MariaDB’s secure-file-priv directory (SHOW VARIABLES LIKE 'secure_file_priv';). Use the command in the next section to import.

Can I ignore the header row?

Yes. Append IGNORE 1 LINES so the column names do not enter the table.

How to map CSV columns to table columns?

List the target columns in parentheses after the table name.This decouples CSV order from schema order and supports partial imports.

What are best practices for large CSV files?

Increase bulk_insert_buffer_size, disable indexes with ALTER TABLE ... DISABLE KEYS;, then re-enable after the load. Wrap the import in a transaction to roll back on error.

Why does LOAD DATA LOCAL INFILE fail?

Your client library may disable LOCAL for security. Enable it with the --local-infile flag or the allowLocalInfile=true connection parameter.

.

Why How to Import CSV Files in MariaDB is important

How to Import CSV Files in MariaDB Example Usage


LOAD DATA LOCAL INFILE '/imports/Orders.csv'
INTO TABLE Orders
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(id, customer_id, order_date, total_amount);

How to Import CSV Files in MariaDB Syntax


LOAD DATA [LOCAL] INFILE 'file_path/Customers.csv'
INTO TABLE Customers
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(id, name, email, created_at);

Common Mistakes

Frequently Asked Questions (FAQs)

Is LOAD DATA INFILE faster than INSERT?

Yes. It bypasses the SQL parser for each row, offering 10–20× speedups on million-row files.

Can I import only selected columns?

Yes. Provide the column list and use user variables for skipped CSV fields, e.g., (id, @skip, email).

How do I handle NULL values in CSV?

Represent NULL as \N in the file or use SET column=NULLIF(@var,'') in the command.

Want to learn about other SQL terms?