Inserting ClickHouse data

Insert the data from the command line

### Insert the data from the command line[​](#insert-data-via-cmd "Direct link to Insert the data from the command line")

Next run the command below to insert the data from the file you downloaded earlier into your table:

```
zcat < hacknernews.csv.gz | ./clickhouse client --query "INSERT INTO hackernews FORMAT CSV"
```

As our data is compressed, we need to first decompress the file using a tool like `gzip`, `zcat`, or similar, and then pipe the decompressed data into `clickhouse-client` with the appropriate `INSERT` statement and `FORMAT`.

Note

When inserting data with clickhouse-client in interactive mode, it is possible to let ClickHouse handle the decompression for you on insert using the `COMPRESSION` clause. ClickHouse can automatically detect the compression type from the file extension, but you can also specify it explicitly.

The query to insert would then look like this:

```
clickhouse-client --query "INSERT INTO hackernews FROM INFILE 'hacknernews.csv.gz' COMPRESSION 'gzip' FORMAT CSV;"
```

When the data has finished inserting you can run the following command to see the number of rows in the `hackernews` table:

```
clickhouse-client --query "SELECT formatReadableQuantity(count(*)) FROM hackernews"
28.74 million
```

inserting data via command line with curl

### inserting data via command line with curl[​](#insert-using-curl "Direct link to inserting data via command line with curl")

In the previous steps you first downloaded the csv file to your local machine using `wget`. It is also possible to directly insert the data from the remote URL using a single command.

Run the following command to truncate the data from the `hackernews` table so that you can insert it again without the intermediate step of downloading to your local machine:

```
clickhouse-client --query "TRUNCATE hackernews"
```

Now run:

```
curl https://datasets-documentation.s3.eu-west-3.amazonaws.com/hackernews/hacknernews.csv.gz | zcat | clickhouse-client --query "INSERT INTO hackernews FORMAT CSV"
```

You can now run the same command as previously to verify that the data was inserted again:

```
clickhouse-client --query "SELECT formatReadableQuantity(count(*)) FROM hackernews"
28.74 million
```

- [Inserting into ClickHouse vs. OLTP databases](#inserting-into-clickhouse-vs-oltp-databases)- [Best practices for Inserts](#best-practices-for-inserts)
    - [Insert in large batch sizes](#insert-in-large-batch-sizes)- [Ensure consistent batches for idempotent retries](#ensure-consistent-batches-for-idempotent-retries)- [Insert to a MergeTree table or a distributed table](#insert-to-a-mergetree-table-or-a-distributed-table)- [Use asynchronous inserts for small batches](#use-asynchronous-inserts-for-small-batches)- [Use official ClickHouse clients](#use-official-clickhouse-clients)- [Prefer the native format](#prefer-the-native-format)- [Use the HTTP interface](#use-the-http-interface)- [Basic example](#basic-example)- [Loading data from Postgres](#loading-data-from-postgres)- [Inserting data from the command line](#inserting-data-from-command-line)
          - [Download the CSV](#download-csv)- [Create the table](#create-table)- [Insert the data from the command line](#insert-data-via-cmd)- [inserting data via command line with curl](#insert-using-curl)

Was this page helpful?