Exporting a table from Amazon RDS into a CSV file
AWS manages Amazon Relational Database Service (RDS). It simplifies cloud relational database setup, operation, and scaling. CSV files store tabular data (numbers and text) in plaintext.
Most spreadsheets and database management systems support this tabular data format. Many businesses export data for backup, analysis, or transfer.
This guide will show you how to export an Amazon RDS table to a CSV file in a few easy steps.
To export a table from Amazon RDS to a CSV file, you can use the mysql
command-line tool or a similar tool such as mysqldump
. Here’s an example of how to do this using the mysql
tool:
- Connect to your Amazon RDS database using the
mysql
command-line tool:
mysql -h -u -p
- Enter your password when prompted.
- Select the database that contains the table you want to export:
use <database-name>;
- Use the
SELECT
andINTO OUTFILE
statements to export the table to a CSV file:
SELECT * FROM INTO OUTFILE '/path/to/file.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';
Replace <table-name>
with the name of the table you want to export and /path/to/file.csv
with the desired file path and name for the CSV file.
- Quit the
mysql
tool:
quit;
The CSV file will be created in the specified location with the data from the table.
Alternatively, you can use the mysqldump
tool to export the table to a SQL file and then use a tool like sed
to extract the data as a CSV file. Here’s an example of how to do this:
- Connect to your Amazon RDS database using the
mysqldump
command-line tool:
mysqldump -h <rds-hostname> -u <username> -p <database-name> <table-name> > /path/to/file.sql
- Enter your password when prompted.
- Extract the data from the SQL file as a CSV file using
sed
:
sed -n '/INSERT INTO/p' /path/to/file.sql | sed 's/),(/\n/g' | sed 's/[^0-9,.-]//g' > /path/to/file.csv
This will create a CSV file in the specified location with the data from the table.