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:

  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.

Add a Comment

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

ABOUT CODINGACE

My name is Nohman Habib and I am a web developer with over 10 years of experience, programming in Joomla, Wordpress, WHMCS, vTiger and Hybrid Apps. My plan to start codingace.com is to share my experience and expertise with others. Here my basic area of focus is to post tutorials primarily on Joomla development, HTML5, CSS3 and PHP.

Nohman Habib

CEO: codingace.com

Request a Quote









PHP Code Snippets Powered By : XYZScripts.com