Create Back Up File docker exec -t phoenixDB pg_dump -U postgres -d phoenix > phoenix_backup.sql # PostgreSQL Database Backup in Docker ## πŸ› οΈ Backup Commands for `phoenixDB` ### 1️⃣ **Dump Database (Including DROP Statements)** ```sh docker exec -t phoenixDB pg_dump -U postgres -d phoenix -c > phoenix_backup.sql ``` πŸ“– Explanation: β€’ docker exec -t phoenixDB β†’ Runs the pg_dump command inside the phoenixDB container. β€’ pg_dump -U postgres -d phoenix -c: β€’ -U postgres β†’ Connect as user postgres. β€’ -d phoenix β†’ Dump the phoenix database. β€’ -c β†’ Includes DROP statements (cleans tables before restoring). β€’ > phoenix_backup.sql β†’ Saves the dump to your host machine. πŸ”Ή When to Use? β€’ Use this when you want to completely overwrite an existing database on restore. β€’ Ensures that old tables are dropped before new ones are created. # Dump Database (Without DROP Statements) ```sh docker exec -t phoenixDB pg_dump -U postgres -d phoenix > phoenix_backup.sql ``` πŸ“– Explanation: β€’ Similar to the previous command, but without -c. β€’ This does NOT include DROP TABLE statements, meaning: β€’ It will fail if restoring to a database that already has tables with the same names. β€’ Use this when restoring to a new empty database. πŸ”Ή When to Use? β€’ If you’re backing up for archival purposes. β€’ If you don’t want to drop existing data on restore. # πŸ› οΈ How to Restore the Backup? ## Restore to a Database (bkp1) ```sh cat phoenix_backup.sql | docker exec -i phoenixDB psql -U postgres -d bkp1 ``` πŸ“– Explanation: β€’ cat phoenix_backup.sql β†’ Reads the dump file. β€’ | β†’ Pipes it into the psql command inside the container. β€’ docker exec -i phoenixDB psql -U postgres -d bkp2 β€’ -i β†’ Allows input redirection. β€’ -U postgres β†’ Connects as the postgres user. β€’ -d bkp2 β†’ Restores to the bkp2 database # 🎯 Final Notes β€’ Use the -c flag when restoring into an existing database and you want to drop old tables first. β€’ Omit -c when restoring into a new, empty database.