# Using COPY TO for CSV

**URL:** https://community.cratedb.com/t/using-copy-to-for-csv/1195
**Category:** CrateDB
**Created:** [August 25, 2022, 11:09am UTC](https://community.cratedb.com/t/using-copy-to-for-csv/1195 "2022-08-25T11:09:42Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![djbestenergy](https://avatars.discourse-cdn.com/v4/letter/d/8c91f0/32.png) [@djbestenergy](https://community.cratedb.com/u/djbestenergy)
#### Post date: [August 25, 2022, 11:09am UTC](https://community.cratedb.com/t/using-copy-to-for-csv/1195/1 "2022-08-25T11:09:43Z")

</div>

Hi,

After looking through docs on the COPY TO/FROM it seems that the COPY TO only does json… ?

 ![image](https://us1.discourse-cdn.com/flex020/uploads/crate/original/1X/0a5d43268e681abfae6718f2aaca3667433e1161.png)

What I’m trying to do is dump one table to CSV, so that I can specify the columns to import using COPY FROM into another table ( which has an extra generated column ).  
JSON does not seem to allow for extra columns … ?  
_( I should note the other table is on another instance elsewhere, otherwise I’d use INSERT INTO () SELECT \* FROM blah )_

I’ve read the various other tutorials but most seem to assume the CSV is generated elsewhere.  
I’ve tried CSV export within DBeaver, but this blows up with memory issues ( 9M+ rows in the table to export ).

Any help will be appreciated.

Many thanks  
David.

---

<div class="post-metadata">

### Author: ![proddata](https://sea2.discourse-cdn.com/flex020/user_avatar/community.cratedb.com/proddata/32/1379_2.png) [@proddata](https://community.cratedb.com/u/proddata)
#### Post date: [August 25, 2022, 12:15pm UTC](https://community.cratedb.com/t/using-copy-to-for-csv/1195/3 "2022-08-25T12:15:03Z")

</div>

Indeed `COPY TO` currently is only supported for JSON files

> [@djbestenergy](#):
>
> JSON does not seem to allow for extra columns … ?

What do you mean by that?

```auto
cr> CREATE TABLE old_table (val integer);                                                                                                                        
CREATE OK, 1 row affected (1.694 sec)

cr> INSERT INTO old_table (val) VALUES (1),(2),(3);                                                                                                              
INSERT OK, 3 rows affected (0.197 sec)

cr> COPY old_table TO DIRECTORY '/Users/Sandbox/crate-5.0.0/export';                                                                                      
COPY OK, 3 rows affected (0.027 sec)

cr> CREATE TABLE new_table (val integer, val_g GENERATED ALWAYS AS val+1);                                                                                       
CREATE OK, 1 row affected (1.556 sec)

cr> COPY new_table FROM '/Users/Sandbox/crate-5.0.0/export/*';                                                                                            
COPY OK, 3 rows affected (0.134 sec)

cr> SELECT * FROM new_table;                                                                                                                                     
+-----+-------+
| val | val_g |
+-----+-------+
| 1 | 2 |
| 2 | 3 |
| 3 | 4 |
+-----+-------+
SELECT 3 rows in set (0.014 sec)

```

---

<div class="post-metadata">

### Author: ![smu](https://sea2.discourse-cdn.com/flex020/user_avatar/community.cratedb.com/smu/32/60_2.png) [@smu](https://community.cratedb.com/u/smu)
#### Post date: [August 25, 2022, 12:19pm UTC](https://community.cratedb.com/t/using-copy-to-for-csv/1195/4 "2022-08-25T12:19:20Z")

</div>

Yes right, `COPY TO` only supports `JSON`.  
There is also a feature request at GH ([export to csv · Issue #8551 · crate/crate · GitHub](https://github.com/crate/crate/issues/8551)), but it lacks a bit the use-case or why post-processing JSON to CSV isn’t an option.

Converting the exported JSONL to CSV could be done e.g. using [jq](https://stedolan.github.io/jq/):  
`jq -r -s '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv' <YOUR_FILE_NAME>.json`

---

<div class="post-metadata">

### Author: ![djbestenergy](https://avatars.discourse-cdn.com/v4/letter/d/8c91f0/32.png) [@djbestenergy](https://community.cratedb.com/u/djbestenergy)
#### Post date: [August 25, 2022, 12:41pm UTC](https://community.cratedb.com/t/using-copy-to-for-csv/1195/5 "2022-08-25T12:41:34Z")

</div>

> [@proddata](#):
>
> ```auto
> cr> CREATE TABLE new_table (val integer, val_g GENERATED ALWAYS AS val+1);                                                                                       
> CREATE OK, 1 row affected (1.556 sec)
> 
> ```

Oh that’s odd - it was not doing anything on import, no matter what I tried and so I assumed that it was due to not specifying the columns and/or the table schema being different to the import.

I’ll have to try again, as it must be something else.

Thanks.

---

<div class="post-metadata">

### Author: ![djbestenergy](https://avatars.discourse-cdn.com/v4/letter/d/8c91f0/32.png) [@djbestenergy](https://community.cratedb.com/u/djbestenergy)
#### Post date: [August 25, 2022, 12:56pm UTC](https://community.cratedb.com/t/using-copy-to-for-csv/1195/6 "2022-08-25T12:56:18Z")

</div>

OK - it was a mistake from my end.

So with the COPY FROM I was specifying the files individually

tableX\_1.json.gz  
tableX\_2.json.gz  
…

which did not do anything ( so therefore I was thinking it was needing to specify columns i.e. CSV ).

So now I tried with a wildcard (\*) in the and its working. Sorry!

Thanks for all the help though.

Best regards  
David.
