This guide provides a quick and manual way to backup and restore a PostgreSQL database running inside a Docker container using pg_dump
and pg_restore
.
Backup PostgreSQL (Dump)
To create a binary-format backup of your PostgreSQL database, run the following command from your host machine:
docker exec postgres_db pg_dump -Fc --user=odoo --dbname=my_db > my_db.dump
What this does:
- Executes
pg_dump
inside the Docker container namedpostgres_db
- Dumps the database
my_db
using the userodoo
- Uses the custom format (
-Fc
) which is suitable for use withpg_restore
- Saves the output to a file named
my_db.dump
in the current directory on the host machine
Restore PostgreSQL
To restore the database from a previously created dump file:
- Copy the dump file into the container:
docker cp "my_db.dump" "postgres_db:/tmp/my_db.dump"
- Run the restore command:
docker exec -i "postgres_db" pg_restore -U "odoo" -C -d postgres "/tmp/my_db.dump"
What this does:
- Restores the database using
pg_restore
inside the container - The
-C
flag tells it to create the database - The dump is restored into the default
postgres
database as a starting point
Version Compatibility Notice
Ensure that the PostgreSQL version used for dumping and restoring belongs to the same major version branch.
Compatible example:
- Dumped with PostgreSQL 16.3
- Restored with PostgreSQL 16.8
Incompatible example:
- Dumped with PostgreSQL 13.5
- Restored with PostgreSQL 16.3
Always verify your PostgreSQL version inside Docker using:
docker exec postgres_db psql -U odoo -c "SELECT version();"
Reply