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

  1. Paste the CSV

    Paste the data into the left box or open the file. The sample has headings with spaces, like Customer ID and Credit Limit, an empty credit limit, and the name O'Brien, Siobhán, which contains both an apostrophe and a comma.

  2. Choose the database and table

    Pick PostgreSQL in Database and type customers in Table name. The message says 4 rows, 6 columns.

  3. Review the CREATE TABLE statement

    Headings have become safe names such as customer_id and joined_on. credit_limit is a number column that allows NULL, because one customer has no limit. active is a boolean.

    CREATE TABLE "customers" (
      "customer_id" TEXT NOT NULL,
      "full_name" TEXT NOT NULL,
      "credit_limit" DOUBLE PRECISION,
      "active" BOOLEAN NOT NULL,
      ...
    );
  4. Adjust types if you need to

    The script is a starting point. joined_on holds plain dates, so you might change its type to DATE, and you may want NUMERIC(12,2) for money or a PRIMARY KEY on customer_id. Make those edits in the downloaded file, or in your database client, before running it.

  5. 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 is NULL.

  6. Run the script

    Press Download to save import.sql, then run it, for example with psql -d shop -f import.sql. For MySQL, switch Database to MySQL / MariaDB first so names are quoted with backticks, then run mysql shop < import.sql.

Try it yourself

Opens the CSV to SQL Converter with this tutorial's sample loaded.

Try it now

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.