CSV to SQL Converter · Tutorial
How to import a CSV file into a PostgreSQL or MySQL table
7 min readintermediateUpdated
You have a customer list as CSV and need it in a database table, but the import wizard keeps failing on apostrophes and empty cells. This tutorial turns the CSV into a ready-to-run SQL script for PostgreSQL or MySQL, explains the column types it chooses and shows how to run it.
What you need
- The CSV to SQL Converter
- A CSV file, or the sample customer list from the Try it button
- A database client such as psql, the MySQL client or a GUI tool
Steps
Paste the CSV
Paste the data into the left box or open the file. The sample has headings with spaces, like
Customer IDandCredit Limit, an empty credit limit, and the nameO'Brien, Siobhán, which contains both an apostrophe and a comma.Choose the database and table
Pick PostgreSQL in Database and type
customersin Table name. The message says 4 rows, 6 columns.Review the CREATE TABLE statement
Headings have become safe names such as
customer_idandjoined_on.credit_limitis a number column that allows NULL, because one customer has no limit.activeis a boolean.CREATE TABLE "customers" ( "customer_id" TEXT NOT NULL, "full_name" TEXT NOT NULL, "credit_limit" DOUBLE PRECISION, "active" BOOLEAN NOT NULL, ... );Adjust types if you need to
The script is a starting point.
joined_onholds plain dates, so you might change its type toDATE, and you may wantNUMERIC(12,2)for money or aPRIMARY KEYoncustomer_id. Make those edits in the downloaded file, or in your database client, before running it.Check the escaped values
In the INSERT, the name is written as
'O''Brien, Siobhán'. The doubled apostrophe is how SQL stores a single one, so the statement runs safely. The missing credit limit isNULL.Run the script
Press Download to save
import.sql, then run it, for example withpsql -d shop -f import.sql. For MySQL, switch Database to MySQL / MariaDB first so names are quoted with backticks, then runmysql shop < import.sql.
Try it yourself
Opens the CSV to SQL Converter with this tutorial's sample loaded.
Common mistakes
- Running CREATE TABLE twice
- If the table already exists, the script stops with an error. Untick Include CREATE TABLE to generate only the INSERT statements.
- Using the wrong dialect
- PostgreSQL rejects backtick quoting and MySQL treats double-quoted names as strings by default. Always choose the database you will run the script on.
- Very large files in one statement
- Keep Rows per INSERT at a few hundred. Huge single statements can exceed the server's packet or statement size limit.
Next steps
Go back to the CSV to SQL Converter, or keep going with one of these:
FAQ
Can I insert into a table in a schema?
Yes. Type the name as sales.customers and both parts are quoted correctly.
Is COPY faster for big files?
For millions of rows, PostgreSQL's COPY or MySQL's LOAD DATA is faster. For everyday files, a generated script is simpler and works in any client.