How to Import a CSV Into PostgreSQL, MySQL, or SQLite
Every SQL database has its own preferred method for importing CSV data. The right approach depends on which database you are using, how large the file is, and how much control you need over the import process. This guide covers the most reliable methods for each of the three most common open-source databases.
Before You Import: Prepare the CSV
Regardless of which database you are using, clean CSV imports start with a well-prepared source file:
Consistent headers: Column names in the first row. No spaces in header names (use underscores: first_name, not first name). No special characters. Headers that match your target table columns, or that you will map explicitly during import.
Consistent data types per column: A column that will be numeric should contain only numeric values. A date column should use a consistent format across all rows — ISO 8601 (YYYY-MM-DD) is the safest choice and is recognized by all three databases.
Null values: Empty cells in a CSV are typically imported as NULL. If your schema does not allow NULLs on certain columns, fill those cells before importing or add a transformation step during import.
Encoding: Save your CSV in UTF-8 encoding. Files saved in Windows-1252 or Latin-1 encoding often produce character corruption errors on import, especially for names with accented characters.
Importing Into PostgreSQL
PostgreSQL's COPY command is the fastest way to load CSV data. It bypasses the query executor and writes directly to the table at disk level:
COPY table_name (col1, col2, col3)
FROM '/path/to/file.csv'
WITH (FORMAT CSV, HEADER TRUE, DELIMITER ',');
The HEADER TRUE option skips the first row. The DELIMITER option can be changed if your file uses a different separator (tab, semicolon).
For importing from a client machine rather than the server, use \copy in psql:
\copy table_name FROM 'local/path/file.csv' WITH (FORMAT CSV, HEADER TRUE);
\copy runs as the connecting user rather than the PostgreSQL server process and can access files on the client machine. It is slightly slower than server-side COPY but does not require server filesystem access.
Sohovi lets you upload your CSV and get an instant data quality report — no setup, no code required.
Importing Into MySQL
MySQL's LOAD DATA INFILE is the equivalent bulk import command:
LOAD DATA INFILE '/path/to/file.csv'
INTO TABLE table_name
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '
'
IGNORE 1 ROWS
(col1, col2, col3);
IGNORE 1 ROWS skips the header row. ENCLOSED BY '"' handles quoted fields. FIELDS TERMINATED BY ',' sets the delimiter.
For client-side files, use LOAD DATA LOCAL INFILE:
LOAD DATA LOCAL INFILE '/local/path/file.csv'
INTO TABLE table_name
...
Note: LOCAL INFILE must be enabled in both the server configuration (local_infile = ON) and the client connection. Many managed MySQL hosts disable this by default for security reasons.
For importing via GUI, MySQL Workbench has a Table Data Import Wizard that accepts CSV files and handles column mapping interactively — the easiest option for non-developers.
Importing Into SQLite
SQLite's CLI .import command imports CSV files directly:
sqlite3 database.db
.mode csv
.import file.csv table_name
If the table does not exist, SQLite creates it with all columns as TEXT type. If it already exists, the import appends rows to the existing table.
To skip the header row when the table already exists:
.import --skip 1 file.csv table_name
SQLite is the most forgiving of the three databases — it does not enforce types strictly and handles delimiter variations gracefully. This makes it easy to get data in, but means you need to handle type casting in your queries after the import.
For non-CLI users, DB Browser for SQLite provides a visual import wizard with column mapping and type assignment.
SQL INSERT Statements: The Portable Alternative
For small files and maximum portability across all databases, generating SQL INSERT statements from the CSV works everywhere without special configuration:
INSERT INTO table_name (col1, col2, col3) VALUES ('val1', 'val2', 'val3');
INSERT INTO table_name (col1, col2, col3) VALUES ('val4', 'val5', 'val6');
This approach is slower (row-by-row execution through the query engine) but works in any SQL client, does not require file system access, and makes the import fully auditable. For files under 10,000 rows, the performance difference is usually not material.
Sohovi's free CSV to SQL generator produces ready-to-run INSERT statements in PostgreSQL, MySQL, SQLite, or MSSQL dialect — with CREATE TABLE included — directly in your browser.
Always test your import on a non-production database first. Run the full import, verify row counts and sample records, then run your quality checks before switching to production. This pattern catches import issues — wrong delimiter, encoding problems, truncated fields — before they affect your live data.
If you're ready to stop guessing about your data quality, Sohovi is built for exactly this. Upload your first CSV free — no credit card, no IT team, no code needed.
Keep Reading
Generate SQL INSERT Statements from CSV Free →