RDS Postgres Database Recovery
Keywords: PG, reboot, killall
Figure out if you need to completely roll back a database or create a new instance of your database. If you create a new instance, the current instance will still exist. If you rollback, it rolls back the currently active database.
Rolling back a database (Taken from https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_PIT.html)
- Sign in to the AWS Management Console and open the Amazon RDS console at https://console.aws.amazon.com/rds/.
- In the navigation pane, choose Databases.
- Choose the DB instance that you want to restore.
- For Actions, choose Restore to point in time.
- The Launch DB Instance window appears.

- Choose Latest restorable time to restore to the latest possible time, or choose Custom to choose a time.
- If you chose Custom, enter the date and time to which you want to restore the instance. Note: Times are shown in Coordinated Universal Time (UTC).
- For DB instance identifier, enter the name of the target restored DB instance.
- Choose other options as needed, such as storage autoscaling.
- Choose Launch DB Instance.
Restoring From a snapshot:
- Sign in: https://console.aws.amazon.com/rds/
- In the navigation pane, choose Snapshots
3.Choose the DB snapshot that you want to restore from. - For Actions, choose Restore Snapshot.

- Engine is Postgres, choose new unique instance name (this is not the name of the database, but the name of the instance)

- Choose Specs for the database:

- For Prod, choose multi-deployment

- Look everything once over and if everything seems appropriate to you, click Restore Database

NOTE:
- A URI is built via the following syntax:
postgres://<username>:<password>@<host>:<port>/<dbname> - Restoring from a snapshot will create a different
host, everything else should be the same - Rolling back a database will have no differences in URI
- You need to warm up the database cache if you plan to use the new instance restored from snapshot
- Can run something like this to warm for the larger tables in shell
tables=('ab_logs' 'app_accesses' 'app_activations' 'app_form_responses' 'app_infos' 'apps' 'emails' 'general_copies' 'payment_notifications' 'pro_subscriptions' 'search_analytics' 'simple_copies' 'user_infos' 'users')
for table in "${tables[@]}"
do
echo "starting ${table}"
psql -X <URI> -c "SELECT pg_prewarm('${table}', last_block:=1000);"
done