How to Import a CSV File Into a SQL Database (4 Reliable Ways)

Import CSV data into MySQL, PostgreSQL, SQLite or SQL Server with generated INSERT statements or native bulk loaders, and avoid type, quoting and NULL mistakes.

Updated September 29, 2026 6 min read

Use the free CSV to SQL ConverterCreate CREATE TABLE and batched INSERT statements from a CSV file

A colleague sends a spreadsheet of customers exported as CSV. You need it in a database table: for a migration, a one-off report, seed data for a test environment, or to join it against existing records. The data is simple, but the details are not. Which column types? What about the empty cells, the apostrophe in "O'Brien Ltd", the postcode that starts with zero?

This guide compares the two broad approaches, generating SQL statements versus using the database's own bulk loader, shows the native commands for four popular databases, and lists the mistakes that most often corrupt imported data.

Two ways to get CSV into a table

Generate SQL statements. Convert the CSV into CREATE TABLE and INSERT statements, then run them in any SQL client. This works everywhere, including hosted databases where you cannot upload files to the server, and the SQL file is easy to review, commit as a migration or seed script, and re-run.

Use a native bulk loader. Every major database has a command that reads CSV directly: COPY in PostgreSQL, LOAD DATA in MySQL, .import in SQLite and BULK INSERT in SQL Server. These are much faster for large files, but they need the table to exist already, and they often need file access or special permissions.

As a rule of thumb: for thousands of rows, especially seed data and small migrations, generated SQL is convenient and portable. For hundreds of thousands or millions of rows, use the bulk loader.

Generate SQL from CSV with our tool

The CSV to SQL Converter turns a CSV into a CREATE TABLE statement plus batched INSERT statements, in your browser.

  1. Open the CSV to SQL Converter and paste your data into CSV input, click Open file for a .csv or .tsv file, or use Load sample.
  2. Set the Table name and choose the SQL dialect: MySQL, PostgreSQL, SQLite or SQL Server. Identifier quoting, types and boolean values follow the dialect.
  3. Choose Rows per INSERT, from 1 (one per row) up to 1000, and the Delimiter (Auto-detect, comma, semicolon, tab or pipe).
  4. Toggle CREATE TABLE, First row is header and Empty cells as NULL as needed.
  5. Review the SQL statements. The first line is a comment summarising the row count and each detected column type. Then Copy or Download the .sql file.

For a small customer list, PostgreSQL output looks like this:

CREATE TABLE "customers" (
  "id" INTEGER,
  "name" VARCHAR(20),
  "email" VARCHAR(40),
  "zip" VARCHAR(10),
  "signup_date" DATE,
  "active" BOOLEAN,
  "balance" DOUBLE PRECISION
);

INSERT INTO "customers" ("id", "name", "email", "zip", "signup_date", "active", "balance") VALUES
(1, 'O''Brien Ltd', 'ap@obrien.example', '02134', '2024-03-01', TRUE, 1250.50),
(2, 'Acme GmbH', NULL, '10115', '2024-04-12', FALSE, 0),
(3, 'Northwind', 'billing@northwind.example', '98101', '2024-05-20', TRUE, 310);

Notice what happened: the apostrophe was escaped by doubling it, the empty email became NULL, and zip stayed text because 02134 has a leading zero. Types are detected from every value in a column: whole numbers become integer types (or BIGINT when values exceed the 32-bit range), decimals become floating-point, true/false columns become booleans, and ISO dates and date-times get date types. Anything else is text sized to the longest value with some headroom.

Review the detected types

Detection is a starting point. Before running the SQL on anything important:

  • Money needs exact decimals. Floating-point types can round amounts. Change a column like balance to NUMERIC(12,2) in PostgreSQL or DECIMAL(12,2) in MySQL and SQL Server.
  • Add constraints. A PRIMARY KEY on id, NOT NULL on required columns and a UNIQUE index on email are business rules a converter cannot guess.
  • Check text lengths. If more data will arrive later, widen VARCHAR columns.

Native bulk loaders

When the table exists and the file is large, load it directly.

PostgreSQL (run in psql; \copy reads the file from your computer rather than the server):

\copy customers FROM 'customers.csv' WITH (FORMAT csv, HEADER true)

MySQL (requires local_infile to be enabled on both client and server):

LOAD DATA LOCAL INFILE 'customers.csv'
INTO TABLE customers
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;

SQLite (in the sqlite3 shell, recent versions):

.import --csv --skip 1 customers.csv customers

SQL Server (2017 and later support FORMAT = 'CSV'; the path is on the server):

BULK INSERT customers
FROM 'C:\imports\customers.csv'
WITH (FORMAT = 'CSV', FIRSTROW = 2);

Many desktop database clients also offer an "Import CSV" wizard that wraps these commands in a dialog. They are a comfortable middle ground for occasional imports.

A useful combination: generate only the CREATE TABLE statement with the converter, adjust the types, run it, then load the data with the bulk loader.

Mistakes that corrupt imported data

Leading zeros disappear

Postcodes, phone numbers and product codes like 00042 must be text columns. If a spreadsheet already converted them to numbers before export, the zeros are gone from the CSV itself. Check the raw file in a text editor or the CSV Viewer.

Empty string versus NULL

An empty cell can mean "unknown" (NULL) or "deliberately blank" (''). They behave differently in queries: WHERE email IS NULL will not find empty strings. Decide which you want before importing; Empty cells as NULL controls it for text columns.

Delimiters and encodings

Spreadsheets in many European locales export with semicolons, because the comma is the decimal separator. Also check the encoding: save as UTF-8 so names like "Müller" and "Zoë" survive.

Quotes and injection

Never build INSERT statements by concatenating raw cell values in your own scripts. A single apostrophe breaks the statement, and hostile data can inject SQL. Generated SQL must escape quotes properly, and application code should use parameterised queries.

Batch size limits

Very large multi-row INSERT statements can exceed a server's maximum packet or statement size. SQL Server accepts at most 1,000 rows in a single VALUES list, so the converter caps batches at 1,000 for that dialect. If a big file fails, lower Rows per INSERT or switch to the bulk loader.

After the import

Run a quick sanity check: SELECT COUNT(*) should match the number of data rows, and a SELECT of a few rows with special characters should look right. Tidy up the SQL file for your repository with the SQL Formatter. If the data needs to go to an API instead of a database, CSV to JSON is the better route.

Frequently asked questions

What is the fastest way to import a large CSV into SQL?

The database's native bulk loader: COPY in PostgreSQL, LOAD DATA in MySQL, .import in SQLite or BULK INSERT in SQL Server. It avoids parsing thousands of individual statements.

How do I escape a single quote in a SQL INSERT?

Double it: 'O''Brien Ltd'. MySQL also treats backslashes as escape characters by default, so backslashes in data must be doubled there as well.

Should I include CREATE TABLE in the generated SQL?

Include it when the table does not exist yet, but review the types first. Leave it out when inserting into an existing table whose structure you already control.

Why did my ZIP codes lose their leading zeros?

They were treated as numbers, either by the importing tool or by the spreadsheet before export. Store them in a text column and make sure the CSV file still contains the zeros.

Can I import a CSV without a header row?

Yes. Switch off First row is header, and the columns are named column_1, column_2 and so on. Rename them in the CREATE TABLE statement before running it.

Try it now: CSV to SQL Converter

Loading tool…