Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting a table from Amazon RDS into a CSV file

I have a MySQL database running in Amazon RDS, and I want to know how to export an entire table to CSV format.

I currently use MySQL server on Windows to query the Amazon database, but when I try to run an export I get an error, probably because there's no dedicated file server for amazon RDS. Is there a solution to this?

like image 394
Kenny Avatar asked Mar 02 '12 15:03

Kenny


People also ask

How do I import data from RDS to Amazon S3?

You can query data from an RDS for PostgreSQL DB instance and export it directly into files stored in an Amazon S3 bucket. To do this, you use the aws_s3 PostgreSQL extension that Amazon RDS provides. The upload to S3 uses server-side encryption by default.

How do I export data from PostgreSQL to Amazon S3?

To export data to an Amazon S3 file, give the RDS for PostgreSQL DB instance permission to access the Amazon S3 bucket that the export will use for storage. Doing this includes the following steps: Create an IAM policy that provides access to an Amazon S3 bucket that you want to export to.

Where does Amazon RDS Save my Files?

AWS Region (optional) – The AWS Region where the Amazon S3 bucket is located. If you don't specify an AWS Region value, then Amazon RDS saves your files into Amazon S3 in the same AWS Region as the exporting DB instance .

How do I export data from the Aurora table to S3?

To export data from the Aurora table to the S3 bucket, use the SELECT INTO OUTFILE S3 The following statement exports the entire table to the S3 bucket. If you’re trying this feature out for the first time, consider using the LIMIT clause for a larger table. See the following code: The following code shows the exported file in the S3 bucket.


1 Answers

Presumably, you are trying to export from an Amazon RDS database via a SELECT ... INTO OUTFILE query, which yields this indeed commonly encountered issue, see e.g. export database to CSV. The respective AWS team response confirms your assumption of lacking server access preventing an export like so, and suggests an alternative approach as well via exporting your data in CSV format by selecting the data in the MySQL command line client and piping the output to reformat the data as CSV, like so:

mysql -u username -p --database=dbname --host=rdshostname --port=rdsport --batch   -e "select * from yourtable"   | sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' > yourlocalfilename 

User fpalero provides an alternative and supposedly simpler approach, if you know and specify the fields upfront:

mysql -uroot -ppassword --database=dbtest   -e "select concat(field1,',',field2,',',field3) FROM tabletest" > tabletest.csv 
like image 74
Steffen Opel Avatar answered Sep 16 '22 14:09

Steffen Opel