how to take pg_dumpall of AWS RDS database

The article provides the commands to take a global dump of AWS RDS database.

To perform dump/restore of AWS RDS database, you can simply run the pg_dump command to take a dump of the desired database and restore it. However, pg_dump takes a dump of the database but it doesn't dump the global objects that are common to all databases. 

pg_dumpall is also used to dump global objects that are common to all databases, namely database roles, tablespaces, and privilege grants for configuration parameters. (pg_dump does not save these objects.)

AWS RDS Postgres (by design) doesn't allow you to read pg_authid, which was earlier necessary for pg_dumpall to work. A patch was committed in PG-10 that allows the non-super user to use pg_dumpall to take a global dump. After the patch. pg_dumpall uses a workaround (pg_roles) to read the same data (except passwords) and generate the same SQL Script

The following pg_dumpall commands is used to take a global dump of RDS Postgres database. We are using --global-only to only dump the global objects also using the --no-role-passwords switch. The --no-role-passwords flag is still necessary since AWS RDS doesn't allow you to read Passwords.

pg_dumpall --globals-only --no-role-passwords \
  -U rdssuperuser -h rdshost \
  2>stderr.txt > test_globals.sql

All the SQL commands for the dump will be directed to test_globals.sql file. Please replace the user and host entries accordingly.

Now you can simply restore this on a local machine :

psql -U postgres -c "CREATE DATABASE test;"

psql -U postgres test < test_globals.sql