Exporting a table from Amazon RDS into a CSV file

Share this post on:

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:

  1. Connect to your Amazon RDS database using the mysql command-line tool:
mysql -h  -u  -p
  1. Enter your password when prompted.
  2. Select the database that contains the table you want to export:
use <database-name>;
  1. Use the SELECT and INTO 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.

  1. 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:

  1. 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
  1. Enter your password when prompted.
  2. 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.

Author: Nohman Habib

I basically work in the CMS, like Joomla and WordPress and in framework like Laravel and have keen interest in developing mobile apps by utilizing hybrid technology. I also have experience working in AWS technology. In terms of CMS, I give Joomla the most value because I found it so much user freindly and my clients feel so much easy to manage their project in it.

View all posts by Nohman Habib >

Leave a Reply

Your email address will not be published. Required fields are marked *

PHP Code Snippets Powered By : XYZScripts.com