DataSwitch Pro Private, in-browser data converter

How to Import a CSV File into MySQL, PostgreSQL, SQL Server or SQLite

By the DataSwitch Pro team · · 4 min read

Quick answer

Each database has a bulk loader for CSV files: LOAD DATA LOCAL INFILE in MySQL, \copy ... WITH (FORMAT csv, HEADER true) in PostgreSQL's psql, BULK INSERT ... WITH (FORMAT = 'CSV', FIRSTROW = 2) in SQL Server 2017 and later, and .import --csv in the SQLite shell. Create the table first with matching columns, make sure the file is UTF-8, and skip the header row. For small files, or when you cannot access the server's file system, generate INSERT statements with a CSV to SQL converter instead.

Loading a spreadsheet export into a database is one of the most common data chores. Every database can do it quickly, but each uses different syntax and has its own permission rules. Below, the same file is loaded four ways.

products.csv

sku,name,price,stock
00412,"Mug, blue",12.50,40
00413,Glass jar,8.90,0

Step 1: Create the table

Bulk loaders need an existing table whose columns match the file. Choose types deliberately: codes such as sku are text, not numbers, or the leading zeros disappear.

SQL (works in all four, with small type differences)

CREATE TABLE products (
  sku   VARCHAR(20) PRIMARY KEY,
  name  VARCHAR(200) NOT NULL,
  price DECIMAL(10,2),
  stock INTEGER
);

MySQL and MariaDB: LOAD DATA

MySQL

LOAD DATA LOCAL INFILE '/path/to/products.csv'
INTO TABLE products
CHARACTER SET utf8mb4
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(sku, name, price, stock);

LOCAL reads the file from your computer rather than the server. It must be enabled on both sides: the server variable local_infile and the client option --local-infile=1. Without LOCAL, the file must be on the server in the directory allowed by secure_file_priv. Files saved on Windows may end lines with \r\n; use that in LINES TERMINATED BY if the last column gets a stray character.

PostgreSQL: \copy

psql

\copy products FROM 'products.csv' WITH (FORMAT csv, HEADER true, ENCODING 'UTF8')

\copy is a psql command that reads the file on your machine and streams it to the server. The SQL command COPY does the same with a file on the server, but requires superuser rights or the pg_read_server_files role. Empty unquoted fields become NULL in CSV mode.

SQL Server: BULK INSERT or the import wizard

T-SQL (SQL Server 2017+)

BULK INSERT dbo.products
FROM 'C:\data\products.csv'
WITH (FORMAT = 'CSV', FIRSTROW = 2, FIELDQUOTE = '"', CODEPAGE = '65001');

The path is read by the SQL Server service, so the file must be on the server or on a share it can access. FORMAT = 'CSV' handles quoted fields; older versions do not, which breaks values containing commas. In SQL Server Management Studio, right-click the database → Tasks → Import Flat File is a friendlier alternative that also creates the table.

SQLite: .import

sqlite3 shell

.import --csv --skip 1 products.csv products

If the table does not exist yet, omit --skip 1: SQLite creates the table and uses the header row as column names (with every column typed as text). The --csv and --skip options need SQLite 3.32 or later.

Alternative: generate INSERT statements

Bulk loaders are fastest, but they need file access and server settings you may not control — on shared hosting with phpMyAdmin, for example. For small and medium files, converting the CSV into INSERT statements is often simpler: open the CSV to SQL converter, choose your database dialect, optionally add a CREATE TABLE, and run the generated script in any SQL client. Quotes inside values are escaped, so O'Brien cannot break the statement.

Problems you are likely to meet

Starting from Excel?

Save the sheet as CSV UTF-8, or skip that step and read how to import Excel data into SQL.

Try it on your own file. Free, private, and processed in your browser.

Frequently asked questions

Which method is fastest for large files?

The native bulk loaders (LOAD DATA, COPY, BULK INSERT, .import) are far faster than INSERT statements for millions of rows.

Why does MySQL say “Loading local data is disabled”?

The local_infile setting is off on the server or client. Enable it on both, or use INSERT statements instead.

Do I need to create the table first?

For MySQL, PostgreSQL and SQL Server's BULK INSERT, yes. SQLite's .import and SSMS's Import Flat File can create it for you.

Can I import a CSV with phpMyAdmin?

Yes: select the table, open the Import tab and choose CSV. For big files, upload limits may apply, so splitting the file or using INSERT scripts helps.

More guides