DataSwitch Pro Private, in-browser data converter

How to Export SQL Query Results to CSV (MySQL, PostgreSQL, SQL Server, SQLite)

By the DataSwitch Pro team · · 4 min read

Quick answer

In PostgreSQL use \copy (SELECT ...) TO 'out.csv' WITH (FORMAT csv, HEADER true); in SQLite run sqlite3 -header -csv app.db "SELECT ..." > out.csv; in MySQL use SELECT ... INTO OUTFILE on the server or a client tool's export button; in SQL Server use SSMS's Save Results As or the bcp utility. If you only have a .sql dump file and no running database, extract its INSERT rows with a SQL to CSV converter.

Sooner or later, someone asks for “the data in Excel”. Every database can write query results to CSV, but the commands differ, and some of them quietly skip the header row or fail to quote values that contain commas. Here is the reliable way for each one.

PostgreSQL

psql

\copy (SELECT id, name, email FROM customers WHERE active) TO 'customers.csv' WITH (FORMAT csv, HEADER true)

\copy runs in the psql client and writes the file on your computer. The SQL command COPY ... TO '/path/file.csv' writes on the database server instead and needs elevated privileges. CSV mode quotes values containing commas, quotes or line breaks automatically.

SQLite

Command line

sqlite3 -header -csv shop.db "SELECT * FROM orders;" > orders.csv

Inside the interactive shell, the same is done with .headers on, .mode csv, .output orders.csv, the query, and .output stdout to switch back.

MySQL and MariaDB

MySQL (file written on the server)

SELECT id, name, email
FROM customers
INTO OUTFILE '/var/lib/mysql-files/customers.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n';

INTO OUTFILE writes on the database server, only into the directory allowed by secure_file_priv, and does not include a header row. On managed or shared hosting it is often disabled. Practical alternatives:

SQL Server

When you only have a .sql dump

Backups and exports from hosting panels are often a .sql file full of INSERT statements. You do not need to install a database to read it: the SQL to CSV converter reads the INSERT INTO ... VALUES rows of a table and turns them into CSV, using the column list as headers. Paste the statements of one table at a time so every row has the same columns.

Opening the CSV in Excel correctly

Try it on your own file. Free, private, and processed in your browser.

Frequently asked questions

How do I include column names in a MySQL export?

INTO OUTFILE has no header option. Use a client export (Workbench, phpMyAdmin, mysql --batch), or add a UNION ALL with a row of column names.

Why does my CSV break when a value contains a comma?

The export did not quote fields. Use CSV mode (PostgreSQL, SQLite), ENCLOSED BY in MySQL, or enable quoting in SSMS.

Can I export directly to Excel instead of CSV?

Some GUI tools can. Otherwise export CSV and convert it to .xlsx, which also preserves text columns such as IDs.

How do I export a very large table?

Use the command-line tools (\copy, bcp, mysql --batch) rather than GUIs, and filter or split the export if Excel's 1,048,576-row limit is a concern.

More guides