I ran into this issue specifically in the context of a Django application deployed on Render, but these steps should work for any stack running locally in a Docker container.
I encountered an error in production that did not occur in my local environment. I needed to debug the issue locally, but I needed a copy of the production database to properly debug it.
My application is deployed on Render, which takes daily backups automatically. The Render docs explain how the backup files are created. I was able to just download the .sql.gz file of the latest backup through the Render dashboard.
If the database is NOT running in a container, you can do something like this:
psql -h <hostname> -p <port> -U <username> -d <database> -f /path/to/local/backup_file.sql
If your database is running inside of a docker container, you need to either:
- copy the file into your docker container
- stream the file contents to the docker command
You can copy the file into docker using the docker cp
command followed by the psql
command above, but I chose to stream the contents to the command instead. This is a simple one-liner.
cat /path/to/local/backup_file.sql | docker exec -i <container name> psql -h <hostname> -p <port> -U <username> -d <database>