How to import a CSV file into MySQL, PostgreSQL, SQL Server and SQLite (4 ways)

2026-11-29来源 dev.toEnglish

You have a customers.csv from a spreadsheet, a CRM export or a client, and you need it in a database table. Every database has a fast built-in loader for this, but each one has its own syntax and its own permission gotcha. Here are the four options I reach for, from fastest to most portable.

The file used in every example:

id,name,email,signup_date,active 1,"Khan, Sara",sara@example.com,2026-03-14,true 2,Ali Raza,ali@example.com,2026-04-02,false 

Note the quoted "Khan, Sara". A comma inside a value is the most common thing that breaks naive imports.

And the target table:

CREATE TABLE customers ( id INT PRIMARY KEY, name VARCHAR(255), email VARCHAR(255), signup_date DATE, active BOOLEAN ); 

1. MySQL / MariaDB: LOAD DATA LOCAL INFILE

LOAD DATA LOCAL INFILE '/path/to/customers.csv' INTO TABLE customers FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 LINES (id, name, email, signup_date, @active) SET active = (@active = 'true'); 
  • OPTIONALLY ENCLOSED BY '"' handles the quoted comma.
  • IGNORE 1 LINES skips the header.
  • MySQL has no true/false text-to-boolean conversion on load, so the column goes through a user variable and is converted in SET.

Gotcha: LOCAL must be enabled on both sides, or you get "Loading local data is disabled":

SET GLOBAL local_infile = 1; -- server, needs admin rights 
mysql --local-infile=1 -u user -p mydb # client 

If the file is exported from Excel on Windows, use LINES TERMINATED BY '\r\n'.

2. PostgreSQL: \copy

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

Run it in psql. \copy reads the file from your machine and streams it to the server.

Gotcha: plain COPY customers FROM '/path/customers.csv' (without the backslash) reads the file from the database server's disk and needs superuser or the pg_read_server_files role. On managed databases (RDS, Supabase, Neon…) you almost always want \copy.

PostgreSQL parses true/false and 2026-03-14 directly, so no conversion is needed.

3. SQL Server: BULK INSERT

BULK INSERT dbo.customers FROM 'C:\data\customers.csv' WITH ( FORMAT = 'CSV', -- SQL Server 2017+; understands quoted fields FIRSTROW = 2, -- skip header FIELDQUOTE = '"', ROWTERMINATOR = '0x0a' ); 

Gotchas:

  • The path is on the SQL Server machine, not your laptop.
  • active is a BIT in SQL Server. Convert true/false to 1/0 first, or load into a staging table with VARCHAR columns and convert with INSERT ... SELECT.
  • Before 2017 there is no FORMAT = 'CSV', and quoted commas will split columns. Use the Import Flat File wizard in SSMS instead.

4. SQLite: .import

sqlite3 app.db sqlite> .import --csv --skip 1 customers.csv customers 

--skip 1 drops the header when the table already exists. If the table does not exist, leave out --skip and SQLite creates the table using the header row as column names (every column becomes TEXT).

When none of these work: generate INSERT statements

Sometimes you can't use a bulk loader:

  • you're on a managed database with no file access and no psql,
  • you need a migration or seed file to commit to the repo,
  • the data has to go through a web UI like phpMyAdmin or Adminer,
  • or it's 50 rows and you just want something you can read and paste.

Then plain INSERT statements are the most portable option. They run anywhere and can be reviewed in a pull request. For the file above:

INSERT INTO customers (id, name, email, signup_date, active) VALUES (1, 'Khan, Sara', 'sara@example.com', '2026-03-14', TRUE), (2, 'Ali Raza', 'ali@example.com', '2026-04-02', FALSE); 

Writing these by hand gets old fast. Every string needs quotes, every ' in a name like O'Neil must be doubled, empty cells should become NULL, and SQL Server wants 1/0 instead of TRUE/FALSE. I built a small browser tool that does this: CSV to SQL. It detects column types, keeps zip codes and phone numbers as text so leading zeros survive, batches rows per INSERT (SQL Server's 1000-row limit is respected), and the data never leaves your browser. (Disclosure: I made it.)

A few rules for generated INSERTs:

  1. Batch rows. One INSERT with 100–1000 rows is many times faster than 1000 single-row statements.
  2. Wrap it in a transaction so a failure halfway doesn't leave half the data:
 BEGIN; -- inserts… COMMIT; 
  1. Oracle (before 23c) has no multi-row VALUES. Use one INSERT per row, or INSERT ALL.

Quick comparison

Database Command File must be on Header skip Watch out for
MySQL LOAD DATA LOCAL INFILE your machine IGNORE 1 LINES local_infile disabled; booleans
PostgreSQL \copy … (FORMAT csv, HEADER) your machine HEADER true COPY vs \copy
SQL Server BULK INSERT … FORMAT='CSV' the server FIRSTROW = 2 needs 2017+; BIT columns
SQLite .import --csv --skip 1 your machine --skip 1 all TEXT if table is auto-created
Any generated INSERTs anywhere n/a escaping, batching

For anything above a few hundred thousand rows, use the native loader. For seeds, migrations and quick one-offs, INSERT statements are hard to beat.

原文:https://www.a8q9.cn/en/article/how-to-import-a-csv-file-into-mysql-postgresql-sql-server-and-sqlite-4-ways-a9f8b4.html

相关文章