Why Export SQL to CSV?
CSV is the lowest-friction hand-off from a database to analysts, spreadsheets, and other systems. Almost every BI tool, Excel, and ETL pipeline can ingest it. Exporting a query—not an entire dump—keeps files focused and shareable.
Pick an Export Path
Server-side exports are fastest. Client tools are easier when you lack filesystem permissions.
| Method | Engine | Notes |
|---|---|---|
| COPY (SELECT ...) TO | PostgreSQL | Fast; server or \copy from client |
| SELECT ... INTO OUTFILE | MySQL | Needs FILE privilege + server path |
| mysql / psql client flags | MySQL / Postgres | No server write access required |
| SSMS / Azure Data Studio | SQL Server | Save results as CSV |
| sqlite3 -csv | SQLite | Simple CLI export |
Step-by-Step: PostgreSQL
COPY TO is the standard high-speed export.
COPY (
SELECT order_id, email, amount, created_at
FROM orders
WHERE created_at >= DATE '2026-01-01'
) TO '/tmp/orders_2026.csv'
WITH (FORMAT csv, HEADER true, ENCODING 'UTF8');From your laptop with \copy
When the database server cannot write to your machine, stream through psql.
\copy (SELECT order_id, email, amount FROM orders) TO 'orders.csv' WITH (FORMAT csv, HEADER true)Step-by-Step: MySQL
INTO OUTFILE writes on the server. Client-side export avoids FILE privilege issues.
SELECT order_id, email, amount
INTO OUTFILE '/var/lib/mysql-files/orders.csv'
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM orders;mysql CLI (no OUTFILE)
Works when you only have SELECT rights.
mysql -u user -p -B -e "SELECT order_id, email, amount FROM app.orders" \
| sed 's/\t/,/g' > orders.csvStep-by-Step: SQL Server and SQLite
GUI and CLI paths cover most day-to-day needs.
SQL Server
In SSMS: run the query → Results grid → right-click → Save Results As → CSV. For automation, use bcp or Export Flat File in SSIS.
SQLite
Built-in CSV mode is enough for small databases.
sqlite3 -header -csv my.db "SELECT * FROM orders;" > orders.csvMake the CSV Excel-Safe
Database exports often open wrong in Excel without a little care.
- Use UTF-8; add a BOM only if Excel on Windows requires it for your locale.
- Quote fields that contain commas, quotes, or newlines.
- Export dates as ISO 8601 (YYYY-MM-DD).
- Keep long IDs as text to avoid scientific notation.
- Include a header row with stable column names.
Real-World Examples
Common export jobs.
Weekly finance extract
A scheduled Postgres job \copy-s invoice lines to S3 for the FP&A team’s Excel models.
Support ticket dump
An engineer exports a filtered SELECT of affected users to CSV, cleans PII columns, and shares via a secure link.
Migration sample
Before a full cutover, teams export 1,000 rows to CSV, convert to the target schema, and validate in the Online CSV Editor.
Common Mistakes
These corrupt trust in the file.
- Forgetting HEADER so the first data row becomes column names in Excel.
- Tab-separated dumps saved as .csv without changing the delimiter story.
- Exporting NULL as the string "NULL" inconsistently.
- Shipping unrestricted SELECT * with PII.
- Opening UTF-8 CSV in Excel via double-click and breaking characters.
Best Practices
Treat exports like an API contract.
- Export explicit column lists, not SELECT *.
- Document delimiter, encoding, and null representation.
- Filter in SQL; do not dump then delete rows by hand.
- Verify row counts against COUNT(*).
- For Excel delivery, convert CSV to XLSX when formatting matters.
Why Use Convert CSV Online?
After the database spit out CSV, Convert CSV Online is free, browser-based, and requires no account for everyday conversions. Preview and clean in the Online CSV Editor, convert to Excel or JSON, or generate INSERT scripts with CSV to SQL for a target database. Client-side workflows work on Windows, macOS, and Linux browsers.
Bridge systems without another dump
Export once from SQL, reshape in the browser, load elsewhere.
Conclusion
Exporting SQL to CSV is a solved problem on every major engine—use COPY, OUTFILE, or client tools, then verify encoding and headers before you hand the file to Excel or another database.
FAQ
How do I export a SQL query to CSV?
In PostgreSQL use COPY (SELECT ...) TO ... WITH (FORMAT csv, HEADER true). In MySQL use SELECT ... INTO OUTFILE or a client export. SQL Server and SQLite have GUI/CLI CSV export options.
How do I export CSV from PostgreSQL without server file access?
Use psql \copy, which streams results to a file on your client machine.
Why does my MySQL OUTFILE fail?
You need the FILE privilege and a path allowed by secure_file_priv. If that is blocked, export from the mysql client instead.
Should I include a header row?
Yes for human and spreadsheet use. Set HEADER true (Postgres) or write headers explicitly in your export tool.
How do I keep Excel from breaking IDs?
Export IDs as text and open via Data → From Text/CSV, or convert the CSV to Excel with typed columns.
Can I turn the CSV back into SQL?
Yes—use Convert CSV Online’s CSV to SQL Converter to generate INSERT statements for another database.
References
Convert your CSV in the browser
Preview, clean, and convert CSV files free with Convert CSV Online—no installation and no account required for everyday conversions.