How to Export SQL Table to CSV [3 Methods]

Exporting SQL tables to CSV files is a common and useful process in data management. CSV files provide a lightweight, universally compatible format for storing and sharing data.

Amelia

By Amelia / Updated on March 17, 2025

Share this: instagram reddit

Exporting data from SQL tables to CSV (Comma Separated Values) files is a common task when working with databases.CSV files are widely used for data exchange and analysis, and exporting to a CSV file makes it possible to share the data, analyze the data in spreadsheet software such as Excel, or simply back up the data. In this article, we will provide you with detailed steps to export data from SQL tables to CSV files.

export sql table to csv

How to Perform SQL Server Export All Tables to CSV

Method 1. Exporting SQL Table to CSV in MySQL

In MySQL, you can use the SELECT INTO OUTFILE command to export a table’s data into a CSV file.

1. Log into MySQL: First, log into the MySQL command-line tool:

mysql -u username -p

2. Choose the Database: Use the USE command to select the database containing the table you want to export:

USE your_database_name;

3. Export the Table: Use the SELECT INTO OUTFILE statement to export the data. The following is a sample query:

SELECT *
INTO OUTFILE '/path/to/your/output.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM your_table_name;

4. Permissions: Ensure that the MySQL server has write permissions to the directory where you are trying to export the file. Otherwise, you will encounter errors.

Method 2. Exporting SQL Table to CSV in PostgreSQL

PostgreSQL also provides an easy way to export tables to CSV using the COPY command.

1. Log into PostgreSQL: Access your PostgreSQL database by running:

psql -U username -d your_database_name

2. Export the Table: Use the COPY command to export the table:

COPY your_table_name TO '/path/to/your/output.csv' WITH CSV HEADER;

Method 3. Exporting SQL Table to CSV in SQL Server

Using bcp Utility:

1. Run bcp Command: From your command line, run the following command to export a table to CSV file:

bcp "SELECT * FROM your_database_name.dbo.your_table_name" queryout "C:\path\to\your\output.csv" -c -t, -T

2. Export Data from SSMS: Alternatively, you can use SQL Server Management Studio (SSMS) to export data:

  • Right-click the database > Tasks > Export Data.

using ssms

  • Select the SQL Server tables as the data source.
  • Choose Flat File Destination as the destination type.
  • Specify the path for the CSV file and complete the export process using the wizard.

Securely and Efficiently Protect Data with AOMEI Cyber Backup(Backup and Recovery)

For the safety of your data, you can use professional backup software. AOMEI Cyber Backup is a reliable tool that provides an intuitive and automated backup solution designed to simplify SQL Server backup management. Of course, it also supports other data types, such as virtual machine backups and centralized Windows PCs/Server backups.

Centralized Backup: It offers a flexible backup method and can back up all the devices within LAN.
Scheduling and Automation: You can set up automatic SQL backups to reduce the risk of data loss due to human error or unexpected events.
Multiple Backup Options: You can store your data in various locations, such as local disks, network drives, or cloud storage, which provides flexibility and redundancy.
Fast Backup and Recovery: It performs auto with fast speed, which is vital for business continuity.

Download Free TrialMicrosoft SQL Server 2005-2022
Centralized and Secure SQL Backup

Backup with AOMEI Cyber Backup

Step 1. In AOMEI Cyber Backup, navigate to Source Device > Microsoft SQL > Add Microsoft SQL. Then click Download proxy program and install the program on the device with SQL Server installed, and then click Already installed proxy and select the proxies you want to add.

add-device

Step 2. Click > Authentication to validate the database instance. You can choose Windows Authentication or SQL Authentication, then enter credentials and click Verify.

sql authentication

Step 3. Navigate to Backup Task and click Create New Task. Choose the backup type as Microsoft SQL Backup, and you can also try virtual machine backup.

Backup Type

Step 4. Select backup methods as full/incremental/differential and specify the backup time as daily/weekly/monthly to have a scheduled SQL database, then your SQL database will be backed up automatically and regularly.

schedule sql backup

Step 5. Click Start Backup to start the backup process. Once it’s finished, you can find the task in the Backup Task tab.

backup sql server database

Restore with AOMEI Cyber Backup

Step 1. Click Backup Task on the left menu bar, locate the task you want to restore, and click > Restore.

Restore

Step 2. Select a backup version that you want to restore a database from. Then you can choose to Restore to original location or Restore to new location.

Restore to Original Location

Step 3. Click Start Restore to begin the restore progress.

Start Restore

Conclusion

Exporting SQL tables to CSV files is a simple and powerful way to convert data from a relational database to a widely accepted format. Whether you are using MySQL, PostgreSQL, or SQL Server, there are tons of tools and methods available to you. With the right method, you can export data automatically or export tables manually for reporting, analysis, or backup purposes.

Amelia
Amelia · Editor
Amelia is an editor from AOMEI. Trained in professional systems, she specializes in solving problems in enterprise databases, virtual machines, and physical environments. With a high sense of responsibility, she is dedicated to protecting business data and security.