DBAtlas · Database operations atlas

Export data (CSV / to file)

Oracle

-- CSV with SQL*Plus (spool)
SET MARKUP CSV ON
SPOOL customer.csv
SELECT id, name, email FROM customer;
SPOOL OFF

-- Logical export with Data Pump (from the shell)
expdp app_user/password tables=customer directory=dir_dmp dumpfile=customer.dmp

For CSV, use SQL*Plus SET MARKUP CSV ON + SPOOL (12.2+). To move schemas/tables, Data Pump (expdp) produces a binary .dmp that is imported into another Oracle database with impdp.

PostgreSQL

-- Server side
COPY (SELECT id, name, email FROM customer) TO '/data/customer.csv' WITH (FORMAT csv, HEADER true);

-- Client side (psql)
\copy (SELECT id, name, email FROM customer) TO 'customer.csv' WITH (FORMAT csv, HEADER true)

COPY ... TO writes a query result to a file (server side, requires privileges); psql \copy writes to a client file. HEADER true adds a header line.

MySQL

-- To a file on the server
SELECT id, name, email FROM customer
INTO OUTFILE '/data/customer.csv'
FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';

-- To the client: mysql -e ... > file (tab-separated)
-- mysql -e "SELECT * FROM customer" shop > customer.tsv

SELECT ... INTO OUTFILE creates the file on the server (requires the secure_file_priv path and the FILE privilege; it cannot overwrite). For client-side output, redirect with mysql -e.

SQL Server

-- Export with bcp (from the shell)
bcp "SELECT id, name, email FROM shop.dbo.customer" queryout customer.csv -c -t, -S server -T

-- or the whole table
bcp shop.dbo.customer out customer.dat -n -S server -T

bcp out/queryout writes data to a file: -c character mode, -t, field terminator, -n native binary format. Use queryout for a query result. 'Results to File' in SSMS is also an option.

MongoDB

# Export CSV (fields must be specified)
mongoexport --db=shop --collection=customer --type=csv --fields=id,name,email --out=customer.csv

# As JSON
mongoexport --db=shop --collection=customer --out=customer.json

mongoexport produces CSV/JSON; for CSV, the columns must be given explicitly with --fields. Use --query to filter. For a full binary backup, mongodump is preferred.

Redis

# Scan keys (SCAN instead of KEYS in production)
redis-cli --scan --pattern 'customer:*' > keys.txt

# For a full data backup, download an RDB snapshot to the client side (non-blocking)
redis-cli --rdb ./dump.rdb

Structured CSV export is not built in. Use --scan (non-blocking) for a key list and an RDB file for the full data: redis-cli --rdb downloads a snapshot generated in the background on the server to a local file; if you want the file on the server, use BGSAVE. SAVE blocks the main thread; do not use it in production. To get the values too, read the scan result in a loop.

Elasticsearch

# Export with elasticdump
elasticdump --input=http://localhost:9200/customer --output=customer.json --type=data

# For large data, paged reads with scroll / point-in-time
GET customer/_search?scroll=1m
{ "size": 1000, "query": { "match_all": {} } }

There is no direct CSV export. To get all data, use elasticdump or page through _search with scroll/PIT (point-in-time). Converting to CSV requires the Logstash csv output or an external tool.

Open in DBAtlas →