To back up everything, including roles, use pg_dumpall:
pg_dumpall -U postgres -f full_backup.sql
-U postgres: Username (use a superuser account)-f full_backup.sql: Output file nameIf you need to include the password non-interactively (not recommended for security reasons), you can set an env variable:
export PGPASSWORD='yourpassword'
pg_dumpall -U postgres -f full_backup.sql
This is essentially a snapshot of the entire PostgreSQL cluster.
This will recreate roles, databases, and data. It must be done with a user that has superuser privileges.
psql -U postgres -f full_backup.sql
You should run the restore on a fresh or empty cluster — it can conflict with existing objects (roles, databases).
You may need to:
If you only want one database, and not all roles, use pg_dump:
pg_dump -U postgres -F c -d your_database -f your_database.dump
-F c: Custom format (useful for pg_restore)-d your_database: The database to dumppg_restore -U postgres -d your_database your_database.dump
This does not include users/roles by default — just the schema and data.
.pgpass or environment variables if needed.