I would like to back up, and then restore, the contents of an entire postgres installtion, including all roles, all databases, everything.
During the restore, the target postgres installation shall be entirely replaced, so that its state is entirely as specified in the backup, no matter what's on the target postgres installation currently.
Existing answers include this and this, but none of them meets my requirements because
- They use
pg_dump
, forcing me to list the databases manually. I don't want that. I want all databases. - They suggest
pg_dumpall
+psql
, which doesn't work if the target installation already has (some part of) the tables; typical errors includeERROR: role "myuser" cannot be dropped because some objects depend on it
, as a result the table is not dropped, and as a result the eventualCOPY
command importing data fails with e.g.ERROR: duplicate key value violates unique constraint
. - They suggest copying on-disk data files, which doesn't work when you want to backup e.g. from version 9.5 and restore into version 9.6.
How can you backup, and restore, everything in a postgres installation, with one command each?