---
title: How to backup AWS RDS Postgres database to a file?
date: '2023-01-19T00:00:00+00:00'
url: https://www.abhinav.co/how-to-backup-postgres-data-using-pg-dump-with-aws-rds
summary: Use pg_dump to copy Postgres database to a file so that you can restore it
  to a different database
tags:
- AWS
- RDS
- Postgres
- PSQL
- pg_dump
author: Abhinav Saxena
---

# How to backup AWS RDS Postgres database to a file?

1. Allow your machine to access RDS - modify your database RDS instance security group to do that. 
    * Add your ip to the relevant security group that has access to RDS.
2. Make a copy of the database using pg_dump
    * ```$ pg_dump -h <RDS dns> -U <RDS username> -w -f <name of dump file .sql> <name of my database>```
    * If you don't specify `-w` flag, you will be prompted for the password.
    * However, `pg_dump` doesn't authenticate with the password if your RDS username doesn't match with your machine username. This is very likely to happen.
    * To fix this, create an environment variable `PGPASSWORD` with the password and specify `-w` as an option. `pg_dump` will authenticate accordingly.
    * Output will be a dump file(.sql)
3. To restore that dump file to a database, do the following.
    * ```$ psql -U <postgresql username> -d <database name> -f <dump file that you want to restore>```

### Reference
1. https://gist.github.com/syafiqfaiz/5273cd41df6f08fdedeb96e12af70e3b
2. http://stackoverflow.com/questions/31881786/how-to-pg-dump-an-rds-postgres-database
3. http://www.thegeekstuff.com/2009/01/how-to-backup-and-restore-postgres-database-using-pg_dump-and-psql/
