Turn a CSV into SQL INSERT statements
Seeding a database, loading fixtures, or migrating a spreadsheet into a table? This tool turns your CSV into a batch of INSERT INTO statements. The header row supplies the column names, each subsequent row becomes one statement, and you choose the destination table name.
Safe, portable output
- Quoting — strings are single-quoted with embedded quotes doubled, so apostrophes never break a statement.
- Types — clean numbers stay unquoted; empty cells become
NULL. - Identifiers — table and column names are double-quoted and sanitized to safe characters.
The output targets standard SQL (PostgreSQL, SQLite and most engines). For MySQL, swap the double-quoted identifiers for backticks. Need the data as records for an app instead? Use the CSV to JSON converter.
How to import a CSV into a database
INSERT statements suit small files and seed scripts. For large files, the bulk loader of your database is much faster. In each example below, the table must already exist, its columns must match the order of the CSV, and the first line of the CSV is treated as a header.
PostgreSQL
Run this in psql. It reads the file from your own machine:
\copy customers FROM 'customers.csv' WITH (FORMAT csv, HEADER true)The SQL COPY command is similar, but it reads a file on the database server.
MySQL and MariaDB
LOAD DATA LOCAL INFILE 'customers.csv'
INTO TABLE customers
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;The local_infile option must be enabled on both client and server. For a file created on Windows, use '\r\n' as the line terminator.
SQLite
Run this in the sqlite3 shell (version 3.32 or later):
.import --csv --skip 1 customers.csv customersIf the table does not exist yet, leave out --skip 1. SQLite then creates the table and uses the first line as column names.
SQL Server
BULK INSERT dbo.customers
FROM 'C:\data\customers.csv'
WITH (FORMAT = 'CSV', FIRSTROW = 2);The path is read by the server, and FORMAT = 'CSV' requires SQL Server 2017 or later.
How to export a SQL query to CSV
PostgreSQL (psql):
\copy (SELECT * FROM customers) TO 'customers.csv' WITH (FORMAT csv, HEADER true)SQLite (sqlite3 shell):
.headers on
.mode csv
.output customers.csv
SELECT * FROM customers;MySQL writes the file on the server, without a header row, and within the secure_file_priv directory:
SELECT * FROM customers
INTO OUTFILE '/var/lib/mysql-files/customers.csv'
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n';Check the result by opening the CSV in the viewer, or use the CSV statistics calculator to compare totals with your table.
FAQ
- How do I import a large CSV into PostgreSQL, MySQL, SQLite or SQL Server?
- Use the bulk loader of your database rather than INSERT statements. PostgreSQL has \copy, MySQL has LOAD DATA LOCAL INFILE, SQLite has .import, and SQL Server has BULK INSERT. The exact commands are on this page.
- How do I convert a CSV file to SQL INSERT statements?
- Paste your CSV or upload a file, set the target table name, and the tool generates one INSERT statement per row using your CSV headers as column names.
- Which SQL dialect does it target?
- The output uses standard, portable SQL with double-quoted identifiers and single-quoted string values. It runs on PostgreSQL, SQLite and most engines; for MySQL you may prefer backticks, which you can find-and-replace.
- How are values quoted?
- Numeric values are left unquoted, empty cells become NULL, and everything else is single-quoted with embedded quotes safely doubled to prevent broken statements.
- Does it handle semicolon or tab separated files?
- Yes. The parser auto-detects commas, semicolons and tabs, so European CSVs and TSV files work without configuration.
- Is my data uploaded anywhere?
- Never. The conversion runs entirely in your browser. Your file is not sent to any server.